編輯: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>
在 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();
}
}
執行後點擊 Button,就會看到帶有圖示與文字的自訂 Dialog。這套「XML 定義外觀、LayoutInflater 撐開、Builder 掛上」的組合,在任何需要自訂版面的場合都能重用。
Android MediaPlayer 教學:動手做一個音樂播放器
要在 App 裡播放音訊或影片,Android 提供了不少途徑,而最最直接的一條路就是 MediaPlayer 類別。它被封裝在 android.media 套件中,可以
Android 手機衛士(八):安裝過程中處理回退按鈕的邏輯
在手機衛士先前的版本升級對話框中:有些使用者暫時不想更新,卻沒有點擊「稍後再說」,而是選擇按下回退鍵。此時的邏輯應該是讓使用者進入 home 介面,而不是停留在 spla
Android 載入 Spinner:非確定性進度條應用
當 App 需要執行一段時間,但無法得知確切進度時,可以使用非確定性進度條,也就是俗稱的 Spinner。它能讓使用者知道系統仍在處理中。 Spinner 的定位 And
自製 TabMenu 彈出選單:封裝成可重複使用的複合控件
用過 UC 瀏覽器 Android 版的人,大概都對它那個分頁式選單有印象。Android 原生的 OptionsMenu 有個先天限制:超過六項就被折進「更多」裡,能呈