天天看点

情景模式的界面设计

一,准备素材

     1.建文件夹drawable(右键点击res,new,folder,folder name为drawable 点击确定按钮),将情景模式所需素材复制黏贴至drawable。

二,改标题

     1.将/SceneMode/AndroidManifest.xml下的activity中label属性连接的资源名所对应的字符串资源内容改为情景模式。

                 SceneMode/AndroidManifest.xml中关于activity的代码为:

<activity
            android:name="cn.edu.bzu.scenemode.activity.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
           

/SceneMode/res/values/strings.xml                SceneMode/res/values/strings.xml中的代码为:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">情景模式</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>
           

三,布局

      1.将原有的TextView按钮及其属性删除。

      2.将TabHost(Composite,TabHost)按钮拖入示意图中。

      3.对每种模式进行单独布局:

            一)普通情景模式:

             1)新建/SceneMode/res/layout/common_scene_model.xml(右键点击layout,new,other,Android,Android XML Values File,File命名为common_scene_model,Root Element选择Relativeyout,点击finish)。

             2)将单选按钮拖入模拟图中。

                      布局文件/SceneMode/res/layout/common_scene_model.xml的代码为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/common_scene_model"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="40dp" >

        <RadioButton
            android:id="@+id/ringAndVibrate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:textSize="20sp"
            android:text="@string/ringAndVibrate" />

        <RadioButton
            android:id="@+id/ring"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="@string/ring" />

        <RadioButton
            android:id="@+id/Vibrate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="@string/Vibrate" />
         <RadioButton
            android:id="@+id/silent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="@string/silent" />
    </RadioGroup>

</RelativeLayout>
           

                     资源文件/SceneMode/res/values/strings.xml的代码为:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">情景模式</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="ringAndVibrate">铃声和振动</string>
    <string name="ring">铃声</string>
    <string name="Vibrate">振动</string>
    <string name="silent">静音</string>

</resources>
           

           3).将主布局与普通情景模式布局进行关联:

                 将/SceneMode/res/layout/activity_main.xml中的第一个LinearLayout及其属性删除,改为 <include layout="@layout/common_scene_model"/>

         二)定时情景模式

             1)新建/SceneMode/res/layout/timing_scenario_model.xml。

             2)将Medium Text按钮拖入模拟图中。

             3)将TimePicker(Time&Date,TimePicker)拖入模拟图中。

             4)将单选按钮拖入模拟图中。

             5)将主布局文件和定时情景模式的布局文件进行关联:

                          将/SceneMode/res/layout/activity_main.xml中的第二个LinearLayout及其属性改为<include layout="@layout/timing_scenario_model"/>。

                     ceneMode/res/layout/timing_scenario_model.xml中的代码为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/timing_scenario_model"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/set_up_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="29dp"
        android:layout_marginTop="17dp"
        android:text="@string/set_up_time"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/set_up_time"
        android:layout_below="@+id/set_up_time"
        android:layout_marginTop="16dp" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/set_up_time"
        android:layout_below="@+id/timePicker1"
        android:layout_marginRight="14dp"
        android:layout_marginTop="52dp" >
    </RadioGroup>

    <RadioButton
        android:id="@+id/ringAndVibrate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/timePicker1"
        android:layout_below="@+id/timePicker1"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="16dp"
        android:checked="true"
        android:text="@string/ringAndVibrate"
        android:textSize="20sp" />

    <RadioButton
        android:id="@+id/ring"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ringAndVibrate"
        android:layout_alignTop="@+id/radioGroup1"
        android:text="@string/ring"
        android:textSize="20sp" />

    <RadioButton
        android:id="@+id/Vibrate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ringAndVibrate"
        android:layout_below="@+id/silent"
        android:text="@string/Vibrate"
        android:textSize="20sp" />

    <RadioButton
        android:id="@+id/silent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ring"
        android:layout_below="@+id/ring"
        android:text="@string/silent"
        android:textSize="20sp" />

</RelativeLayout>
           

                      /SceneMode/res/values/strings.xml中的代码为:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">情景模式</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="ringAndVibrate">铃声和振动</string>
    <string name="ring">铃声</string>
    <string name="Vibrate">振动</string>
    <string name="silent">静音</string>
    <string name="set_up_time">设置时间</string>
</resources>
           

                三)自定义情景模式:

                           1)新建/SceneMode/res/layout/custom_scenarios.xml。

                           2)将两个Medium Text拖入模拟图中。

                          3)将ProgressBar拖入模拟图中。

                          4)在模拟图中拖入五个ImageView(Images&Media)。

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">情景模式</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="ringAndVibrate">铃声和振动</string>
    <string name="ring">铃声</string>
    <string name="Vibrate">振动</string>
    <string name="silent">静音</string>
    <string name="set-up time">设置时间</string>

</resources>
           

                          5)将主布局文件和自定义布局文件进行关联:

                                           将/SceneMode/res/layout/activity_main.xml中的第三个LinearLayout改为<include layout="@layout/custom_scenarios"/>

                                      /SceneMode/res/layout/custom_scenarios.xml中的代码为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/custom_scenarios"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/contextual_model"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="69dp"
        android:layout_marginTop="57dp"
        android:drawableRight="@drawable/normal"
        android:text="@string/contextual_model"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageButton
        android:id="@+id/mute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/up"
        android:layout_alignTop="@+id/normal"
        android:src="@drawable/mute" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Sound_volume"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/Sound_volume"
        android:max="100"
        android:progress="70" />

    <ImageView
        android:id="@+id/down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/contextual_model"
        android:layout_centerVertical="true"
        android:src="@drawable/down" />

    <TextView
        android:id="@+id/Sound_volume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/contextual_model"
        android:layout_below="@+id/contextual_model"
        android:layout_marginTop="28dp"
        android:text="@string/Sound_volume"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/down"
        android:layout_centerHorizontal="true"
        android:src="@drawable/up" />

    <ImageButton
        android:id="@+id/normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/down"
        android:layout_marginTop="27dp"
        android:layout_toLeftOf="@+id/mute"
        android:src="@drawable/normal" />

    <ImageButton
        android:id="@+id/vibrate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/mute"
        android:layout_toRightOf="@+id/contextual_model"
        android:src="@drawable/vibrate" />

</RelativeLayout>
           

                            /SceneMode/res/values/strings.xml中的代码为:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">情景模式</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="ringAndVibrate">铃声和振动</string>
    <string name="ring">铃声</string>
    <string name="Vibrate">振动</string>
    <string name="silent">静音</string>
    <string name="set_up_time">设置时间</string>
    <string name="contextual_model">情景模式:</string>
    <string name="Sound_volume">声音音量:</string>

</resources>
           

       4.主布局文件的代码为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/bg"
    tools:context=".MainActivity" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <include layout="@layout/common_scene_model"/>

                <include layout="@layout/timing_scenario_model"/>
                <include layout="@layout/custom_scenarios"/>
               
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>
           

四:/SceneMode/src/cn/edu/bzu/scenemode/activity/MainActivity.java中的代码为:

package cn.edu.bzu.scenemode.activity;

import java.util.TimerTask;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.view.Menu;
import android.widget.TabHost;
public class MainActivity extends TabActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TabHost tabHost = getTabHost();
		TabHost.TabSpec normalTab = tabHost.newTabSpec("tab1");
		normalTab.setIndicator("普通情景模式",
				getResources().getDrawable(R.drawable.normal)).setContent(
				R.id.common_scene_model);
		tabHost.addTab(normalTab);
		TabHost.TabSpec timerTab = tabHost.newTabSpec("tab2");
		timerTab.setIndicator("定时情景模式",
				getResources().getDrawable(R.drawable.time)).setContent(
				R.id.timing_scenario_model);
		tabHost.addTab(timerTab);
		TabHost.TabSpec selfDefinationTab = tabHost.newTabSpec("tab3");
		selfDefinationTab.setIndicator("自定义情景模式",
				getResources().getDrawable(R.drawable.selfdefination)).setContent(
				R.id.custom_scenarios);
		tabHost.addTab(selfDefinationTab);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
           

继续阅读