編輯:關於 Android 開發
在實際開發中,LayoutInflater 是非常有用的一個類別,它的作用類似於 findViewById(),但兩者仍有明顯差異:LayoutInflater 用來找 layout 下的 XML 版面配置檔並將其實例化,而 findViewById() 則是在具體的 XML 之下尋找具體的 widget 控制元件(如 Button、TextView 等)。
本文以下面的 Demo 說明:主版面 main.xml 中有一個 TextView 與一個 Button,點擊 Button 後彈出一個 Dialog,而這個 Dialog 的版面配置則使用我們在 layout 目錄下定義的 custom_dialog.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>
<?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>
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)皆可擇一使用。
Android 源碼解析(五)——Log 日誌類的底層實現全解讀
從 framework 源碼走進 Log 的內部世界 本系列走到第五篇,已經正式進入 Android framework 層的源碼地帶。在動手翻閱之前,先推薦一個非常好用
Notification 通知欄訊息實作(含自訂版面)
一個很典型的場景:使用者開啟 Activity 後按下 Home 鍵,此時流程會走 onPause() → onStop(),介面不可見。我們就在這個時機送一則 No
開源專案 BingoWorld:一款用來收集技術文章的 IT 閱讀學習 App
BingoWorld 是一款 IT 閱讀學習類的開源軟體,目標很明確:把散落在網路上的好技術文章集中收藏,方便隨時翻閱。內容規劃涵蓋 Android、iOS 技術文章與相
Android Log 詳解:Log.v / Log.d / Log.i / Log.w / Log.e 全解析
Android Log 詳解:Log.v / Log.d / Log.i / Log.w / Log.e 全解析 在 Android 開發社群裡,經常有人詢問 Andro