2012年5月16日 星期三

[Android] [JAVA] 切換layout (setContentView)





1). 設置兩個layout,分別是main.xml、mylayout.xml
layout1設置一個button,可跳到layout2;layout2相同。並設置layout2的背景為灰色

2). 設定layout1的Button1監聽並觸發到跳到layout2的method

3). 設定layout2的Button2監聽並觸發到跳到layout1的method

//=============================================================


[values/color.xml]
//設定背景顏色
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <drawable name="white">#ffffff</drawable>
  <drawable name="darkgray">#808080</drawable>
</resources>


//-------------------------------------------------------------------------------------------------------------


[values/strings.xml]

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ChangePage1Activity!</string>
    <string name="app_name">ChangePage1</string>
    <string name="layout1_test">This is Loyout 1!!</string>
    <string name="layout1_button">Go To Layout 2</string>
    <string name="layout2_test">This is Loyout 2!!</string>
    <string name="layout2_button">Go To Layout 1</string>
</resources>



//-------------------------------------------------------------------------------------------------------------


[main.xml]
// 第一個layout
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >


  <TextView
    android:id="@+id/layout1_test"
    android:layout_width="338dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.02"
    android:text="@string/layout1_test"
    android:textAppearance="?android:attr/textAppearanceLarge" />


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


  // 第一個按鈕
  <Button
    android:id="@+id/layout1_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/layout1_button" />


  </LinearLayout>
</LinearLayout>


//-------------------------------------------------------------------------------------------------------------


[mylayout.xml]
// 第二個layout
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" 
  android:background="@drawable/darkgray" > //設置背景顏色


  <TextView
    android:id="@+id/layout2_test"
    android:layout_width="338dp"
     android:layout_height="wrap_content"
    android:layout_weight="0.02"
    android:text="@string/layout2_test"
    android:textAppearance="?android:attr/textAppearanceLarge" />


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


  // 第二個按鈕
  <Button
    android:id="@+id/layout2_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/layout2_button" />


    </LinearLayout>
</LinearLayout>


//-------------------------------------------------------------------------------------------------------------


[ChangePage1Activity.java]
//利用setContentView來切換layout
package com.changepage1;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class ChangePage1Activity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //取得資源類別的介面元件
    Button b1 = (Button) findViewById(R.id.layout1_button);
    b1.setOnClickListener(new Button.OnClickListener(){
      //按下按鈕跳到layout2
      @Override
      public void onClick(View v) {
      jumpToLayout2();
      }
    });
  }

  private void jumpToLayout2() {
    // 設定使用mylayout.xml這個layout
    setContentView(R.layout.mylayout);
    Button b2 =(Button)findViewById(R.id.layout2_button);
    b2.setOnClickListener(new Button.OnClickListener(){
      //進入jumpToLayout1()
      @Override
      public void onClick(View v) {
        jumpToLayout1();
      }
    });
  }

  private void jumpToLayout1() {
    // 設定使用main.xml這個layout
    setContentView(R.layout.main);

    Button b1 = (Button) findViewById(R.id.layout1_button);
    b1.setOnClickListener(new Button.OnClickListener(){
      //進入jumpToLayout2(),形成迴圈
      @Override
      public void onClick(View v) {
        jumpToLayout2();
      }
    });
  }
}


沒有留言:

張貼留言