Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

用 setContentView 在手機頁面上做切換

編輯:進階開發

網頁世界裡要在兩個頁面間跳轉,一個超連結就搞定。手機上呢?最簡單的答案其實是:換掉 Activity 的 Layout。

這個例子準備兩套版面——Layout1(main.xml)與 Layout2(mylayout.xml)。預設顯示 main.xml,裡頭放一顆按鈕,點下去切換到 mylayout.xml;反過來,mylayout.xml 上也有一顆按鈕,點它就能切回 main.xml。以下示範這兩頁如何互相來回。

一、主介面 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="歡迎來到魏祝林的部落格" />

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點擊進入 Layout2" />
</LinearLayout>

二、第二個版面 mylayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    android:background="#ffffffff">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Welcome to Mr Wei's blog" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點擊進入 Layout1" />
</LinearLayout>

三、核心程式

setContentViewDemo.java 的做法很直白:每次切換版面就重新呼叫一次 setContentView,然後重新取得按鈕並掛上事件。

package com.android.setContentViewDemo;

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

public class setContentViewDemo extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 載入 main.xml
        setContentView(R.layout.main);

        Button bt1 = (Button) findViewById(R.id.bt1);
        bt1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                goToLayout2();
            }
        });
    }

    // 由 main.xml 切換成 mylayout.xml
    public void goToLayout2() {
        setContentView(R.layout.mylayout);

        Button b2 = (Button) findViewById(R.id.bt2);
        b2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                goToLayout1();
            }
        });
    }

    // 由 mylayout.xml 切換回 main.xml
    public void goToLayout1() {
        setContentView(R.layout.main);

        Button bt1 = (Button) findViewById(R.id.bt1);
        bt1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                goToLayout2();
            }
        });
    }
}

要留意的是,setContentView 換掉的是「內容檢視」,Activity 本身並沒有被替換,因此所有狀態與變數都還留在同一個 instance 上。這讓它很適合切換同一個功能下的不同視圖;但如果兩頁代表的是真正獨立的職責,該用的是 Intent 開啟另一個 Activity,而不是反覆重設內容檢視。此外,每次切換都會重建整棵檢視樹,頻繁呼叫時版面較複雜就可能感覺到卡頓,那個時候就該考慮改用 ViewStub 或 Fragment 了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved