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

說說 LayoutInflater:它與 findViewById 的差別在哪

編輯:Android 開發實例

在實際開發中,LayoutInflater 是一個非常有用但常被忽略的類別。它的作用有點類似 findViewById(),但兩者其實在處理完全不同層次的東西。

核心差異

  • LayoutInflater 是用來找 layout 底下的整個 XML 版面配置檔,並把它實例化成 View 物件樹。
  • findViewById() 是在已經存在的 XML 版面中,找出其中某個具體的 widget 控制項(如 Button、TextView)。

一句話總結:LayoutInflater 負責「把 XML 變成 View」,findViewById 負責「從 View 裡挑出某個元件」。前者是后者的前提——你必須先 inflate 出一份版面,才能在它上面 findViewById。

Demo:點擊按鈕彈出自訂 Dialog

主版面 main.xml 裡有一個 TextView 和一個 Button。點擊 Button 後彈出 Dialog,而這個 Dialog 的版面來自我們另外定義的 custom_dialog.xml(左右分布:左邊 ImageView,右邊 TextView)。

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="@string/hello"
/>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ShowCustomDialog"/>
</LinearLayout>

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="10dp">
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginRight="10dp" />

<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textColor="#7CFC00" />
</LinearLayout>

Activity01.java

package eoe.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Activity01 extends Activity implements OnClickListener{
    private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button)findViewById(R.id.myButton);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        showCustomDialog();
    }

    public void showCustomDialog(){
        AlertDialog.Builder builder;
        AlertDialog alertDialog;
        Context mContext = Activity01.this;

        // 以下 3 種方法都可以取得 LayoutInflater
        // LayoutInflater inflater = getLayoutInflater();
        // mInflater = LayoutInflater.from(this);
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.custom_dialog, null);

        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText("Hello, Welcome to xh blog");

        ImageView image = (ImageView) layout.findViewById(R.id.image);
        // 給 ImageView 設置圖標
        image.setImageResource(R.drawable.icon);

        builder = new AlertDialog.Builder(mContext);
        builder.setView(layout);
        alertDialog = builder.create();
        alertDialog.show();
    }
}

三種取得 LayoutInflater 的方式

程式碼中列出了三條路徑,效果相同,可依情境挑選:

  • getLayoutInflater():Activity 的便利方法,最簡潔。
  • LayoutInflater.from(context):靜態工廠方法,在非 Activity 的類別中特別好用。
  • getSystemService(LAYOUT_INFLATER_SERVICE):最底層的做法,需自行轉型。

inflate() 的兩個參數與第三參數的陷阱

範例用的是 inflate(R.layout.custom_dialog, null) 兩參數版本。更完整的簽名是三參數:

inflate(int resource, ViewGroup root, boolean attachToRoot)
  • resource:要載入的版面配置資源 ID。
  • root:可能成為新 View 父容器的物件,主要用來套用 LayoutParams。
  • attachToRoot:是否要把 inflate 出來的 View 直接掛到 root 上。

這裡有個實務上很值得知道的細節:當你傳入 null 作為 root 時,根元件的 layout_width 與 layout_height 屬性會被忽略,因為沒有父容器可供解析 LayoutParams。若發現自訂 Dialog 的版面尺寸不如預期,第一個該檢查的就是這裡。較好的寫法是把 root 傳進來但設 attachToRoot 為 false。

流程回顧

整個自訂 Dialog 的關鍵就三步:inflate 出版面 → 在這個 layout 上 findViewById 取得子元件並設定內容 → 用 builder.setView(layout) 把它交給 Dialog。注意最後的 findViewById 是呼叫在 layout 上而不是 Activity 上,因為那些元件屬於新載入的版面,Activity 本身的 view 樹裡並沒有它們。

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