Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android ViewStub 用法詳解:需要時才載入的零尺寸佔位元件

編輯:關於 Android 程式設計

ViewStub 的用途是在執行階段才把版面 lazily inflate 進來。官方文件的定義是:

"A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views. Therefore, the ViewStub exists in the view hierarchy until setVisibility(int) or inflate() is invoked. The inflated View is added to the ViewStub's parent with the ViewStub's layout parameters. Similarly, you can define/override the inflate View's id by using the ViewStub's inflatedId property."

歸納起來就是:ViewStub 先替其他 View 卡好位置,等到真正需要時,才呼叫 inflate() 或 setVisibility() 把這些元件載入並顯示出來。

版面檔

layout.xml:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button android:id="@+id/showBtn"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:layout_weight="1"
            android:text="show" />
        <Button android:id="@+id/deleteBtn"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:layout_weight="1"
            android:text="delete" />
    </LinearLayout>
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ViewStub android:id="@+id/viewstub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/next" />
    </LinearLayout>
</LinearLayout>

next.xml 就是被延後載入的那份版面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ProgressBar android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

程式碼

package com.example.android_viewstub1;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;

public class MainActivity extends Activity {
    Button btn1, btn2;
    ViewStub viewStub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) this.findViewById(R.id.showBtn);
        btn2 = (Button) this.findViewById(R.id.deleteBtn);
        viewStub = (ViewStub) this.findViewById(R.id.viewstub);

        btn1.setOnClickListener(new OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(View arg0) {
                // 設為 VISIBLE 即觸發 inflate,版面被真正載入
                viewStub.setVisibility(View.VISIBLE);
            }
        });

        btn2.setOnClickListener(new OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(View arg0) {
                viewStub.setVisibility(View.INVISIBLE);
            }
        });
    }
}

執行結果如下:

什麼時候該用

ViewStub 的價值在於「省下不必要的 inflate 成本」。若某個區塊在大多數使用情境下都不會出現——例如錯誤提示區、空狀態畫面、少見的進階設定——把它包成 ViewStub,就能讓首次載入版面更快、記憶體佔用更低。

有兩個特性值得記住:ViewStub 本身不可見且尺寸為零,而且 inflate 之後它就會把自己從 hierarchy 中移除,因此同一個 ViewStub 只能載入一次;若需要重複顯示,請用一般 View 搭配 visibility 切換,而不是重複 inflate。另外,被載入的 View 會沿用 ViewStub 的 LayoutParams,並可透過 android:inflatedId 覆寫其 id,方便之後用 findViewById 取得。

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