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

Android 說說 LayoutInflater 的用法

編輯:關於 Android 開發

在實際開發中,LayoutInflater 是非常有用的一個類別,它的作用類似於 findViewById(),但兩者仍有明顯差異:LayoutInflater 用來找 layout 下的 XML 版面配置檔並將其實例化,而 findViewById() 則是在具體的 XML 之下尋找具體的 widget 控制元件(如 Button、TextView 等)。

本文以下面的 Demo 說明:主版面 main.xml 中有一個 TextView 與一個 Button,點擊 Button 後彈出一個 Dialog,而這個 Dialog 的版面配置則使用我們在 layout 目錄下定義的 custom_dialog.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="@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>

三、Activity 程式碼

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;

        // 以下三種方法都可以取得 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);
        image.setImageResource(R.drawable.icon); // 為 ImageView 設定圖示

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

關鍵在於:以 LayoutInflater 取得 inflater 後,透過 inflate 將 custom_dialog.xml 實例化為 View,再用 findViewById 在這個 View 中取得控制元件,最後透過 builder.setView(layout) 將自訂版面套用到 Dialog 上。取得 inflater 的三種方式(getLayoutInflater、LayoutInflater.from、getSystemService)皆可擇一使用。

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