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

LayoutInflater 是什麼?用自訂 Dialog 版面實作一遍

編輯:Android 開發實例

實際開發時,LayoutInflater 是個非常實用的類別。它的作用有點像 findViewById(),但兩者分工不同:LayoutInflater 負責「找 layout 下的 XML 版面檔並把它實例化」,而 findViewById() 是「在已實例化的版面中找出具體的 widget 控制項」,例如 Button、TextView。

下面用一個小 Demo 把差別講清楚。主版面 main.xml 裡有一個 TextView 和一個 Button;點擊 Button 後彈出一個 Dialog,而這個 Dialog 的版面,我們另外寫在 layout 目錄下的 custom_dialog.xml 裡(左右配置:左邊 ImageView,右邊 TextView)。

步驟一:建立工程並修改主版面

新增一個 Android 工程,命名為 LayoutInflaterDemo,在 main.xml 中補上一顆 Button:

<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/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ShowCustomDialog" />
</LinearLayout>

步驟二:定義 Dialog 的版面

在 layout 目錄下新增 custom_dialog.xml:

<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="#FFF" />
</LinearLayout>

步驟三:在程式碼中把版面撐開來

public class LayoutInflaterDemo extends Activity implements OnClickListener {

    private Button button;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

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

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

        // 取得 LayoutInflater 有兩種寫法,效果相同
        // LayoutInflater inflater = getLayoutInflater();
        LayoutInflater inflater = (LayoutInflater)
            mContext.getSystemService(LAYOUT_INFLATER_SERVICE);

        // 關鍵一步:把 XML 版面實例化成 View 物件
        View layout = inflater.inflate(R.layout.custom_dialog, null);

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

        ImageView image = (ImageView) layout.findViewById(R.id.image);
        image.setImageResource(R.drawable.icon);

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

重點回顧

  • 取得 LayoutInflater 有兩種方式:呼叫 Activity 的 getLayoutInflater(),或透過 getSystemService(LAYOUT_INFLATER_SERVICE)。
  • inflate() 會把 XML 描述轉成真正的 View 樹,之後才能對內部的控制項呼叫 findViewById()。
  • 把實例化好的 View 交給 AlertDialog.Builder.setView(),自訂版面的對話框就完成了。

執行後點擊 Button,就會看到帶有圖示與文字的自訂 Dialog。這套「XML 定義外觀、LayoutInflater 撐開、Builder 掛上」的組合,在任何需要自訂版面的場合都能重用。

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