編輯:Android 開發實例
在實際開發中,LayoutInflater 是一個非常有用但常被忽略的類別。它的作用有點類似 findViewById(),但兩者其實在處理完全不同層次的東西。
一句話總結:LayoutInflater 負責「把 XML 變成 View」,findViewById 負責「從 View 裡挑出某個元件」。前者是后者的前提——你必須先 inflate 出一份版面,才能在它上面 findViewById。
主版面 main.xml 裡有一個 TextView 和一個 Button。點擊 Button 後彈出 Dialog,而這個 Dialog 的版面來自我們另外定義的 custom_dialog.xml(左右分布:左邊 ImageView,右邊 TextView)。
<?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;
// 以下 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();
}
}
程式碼中列出了三條路徑,效果相同,可依情境挑選:
getLayoutInflater():Activity 的便利方法,最簡潔。LayoutInflater.from(context):靜態工廠方法,在非 Activity 的類別中特別好用。getSystemService(LAYOUT_INFLATER_SERVICE):最底層的做法,需自行轉型。範例用的是 inflate(R.layout.custom_dialog, null) 兩參數版本。更完整的簽名是三參數:
inflate(int resource, ViewGroup root, boolean attachToRoot)
這裡有個實務上很值得知道的細節:當你傳入 null 作為 root 時,根元件的 layout_width 與 layout_height 屬性會被忽略,因為沒有父容器可供解析 LayoutParams。若發現自訂 Dialog 的版面尺寸不如預期,第一個該檢查的就是這裡。較好的寫法是把 root 傳進來但設 attachToRoot 為 false。
整個自訂 Dialog 的關鍵就三步:inflate 出版面 → 在這個 layout 上 findViewById 取得子元件並設定內容 → 用 builder.setView(layout) 把它交給 Dialog。注意最後的 findViewById 是呼叫在 layout 上而不是 Activity 上,因為那些元件屬於新載入的版面,Activity 本身的 view 樹裡並沒有它們。
Android 開發實作:打造仿支付寶錢包的九宮格 GridView
本文要帶大家實作一個仿支付寶錢包首頁、每個格子之間帶有分割線的 GridView,也就是俗稱的九宮格效果。先來看看成品長什麼樣子:我們都知道,ListView 要加分割線
Android Fragment 多級跳轉與逐級返回的完整實現方案
Fragment 的返回鍵管理是個經典難題:頁面一層層往下跳,按返回要一級級退回去,中間某一層可能還想攔截返回事件做別的事(比如先關菜單而不是退出)。本文給出一套接口 +
Android 個人理財實作之三:新增帳單頁面的資料層設計(上)
ColaBox 的記帳功能走到這一步,終於要面對真正的硬骨頭了。新增帳單這個畫面,我翻了不少資料,最後乾脆把 Android 原始碼打開來讀,才總算把它做出來——那一刻才
Android Virtual Device 建立與啟動教學
AVD 是什麼 Android Virtual Device 是 Android 模擬器的虛擬裝置設定。每個 AVD 可指定系統版本、螢幕配置與硬體參數,讓開發者模擬不同