修改strings.xml
<?xml version="1.0" encoding="utf-8"?><resources><string name="layout1">this is Layout 1</string><string name="layout2">This is Layout 2</string><string name="app_name">Ex8_UI</string></resources>
建立color.xml 儲存兩個顏色值 (放到res/values目錄中)
<?xml version="1.0" encoding="utf-8"?><resources><color name="black">#000000</color><color name="white">#FFFFFFFF</color></resources>
修改main.xml 布局,添加一個TextView 和一個Button
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/black"xmlns:android="http://schemas.android.com/apk/res/android"><TextViewandroid:id="@+id/text1"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:text="@string/layout1"></TextView><Buttonandroid:id="@+id/button1"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"android:layout_y="82px"android:text="Go to Layout2"></Button></AbsoluteLayout>
建立mylayout.xml 布局檔案(放到res/layout目錄中),並添加兩個View:TextView 和Button
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/white"xmlns:android="http://schemas.android.com/apk/res/android"><TextViewandroid:id="@+id/text2"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:textColor="@color/black"android:text="@string/layout2"> </TextView><Buttonandroid:id="@+id/button2"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"android:layout_y="82px"android:text="Go to Layout1"></Button></AbsoluteLayout>
編寫mainActivity.java
/** To change this template, choose Tools | Templates* and open the template in the editor.*/package org.me.setcontentviewapp;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;/**** @author LiaoKeCheng<http://hi.baidu.com/wishwingliao>*/public class MainActivity extends Activity{ /** Called when the activity is first created. */ @Override public void onCreate( Bundle icicle ) { super.onCreate( icicle ); /* 載入main.xml Layout */ setContentView( R.layout.main );// 預設啟動布局 /* 以findViewById()取得Button 對象,並添加onClickListener */ Button b1 = ( Button ) findViewById( R.id.button1 ); b1.setOnClickListener( new Button.OnClickListener() { public void onClick( View v ) { jumpToLayout2();// 調用跳轉方法jumpToLayout2() } } ); } /** * method jumpToLayout2:將layout由 main.xml 切 換成mylayout.xml */ public void jumpToLayout2() { /* 將layout 改成mylayout.xml */ setContentView( R.layout.mylayout ); /* 以findViewById()取得Button 對象,並添加onClickListener */ Button b2 = ( Button ) findViewById( R.id.button2 ); b2.setOnClickListener( new Button.OnClickListener() { public void onClick( View v ) { jumpToLayout1();// 調用跳轉方法jumpToLayout1() } } ); } /** * method jumpToLayout1:將layout 由mylayout.xml 切換成main.xml */ public void jumpToLayout1() { /* 將layout 改成main.xml */ setContentView( R.layout.main ); /* 以findViewById()取得Button 對象,並添加onClickListener */ Button b1 = ( Button ) findViewById( R.id.button1 ); b1.setOnClickListener( new Button.OnClickListener() { public void onClick( View v ) { jumpToLayout2();// 調用跳轉方法jumpToLayout2() } } ); }}