編輯:關於 Android 程式設計
setContentView() 與 inflate() 經常被搞混,因為它們都跟「把 XML 變成畫面」有關。但兩者的角色完全不同:一個是「把某個 View 設為 Activity 的內容並立刻顯示」,另一個只是「把 XML 實體化成 View 物件,先拿在手上」。
LayoutInflater 存在的唯一目的,就是把 XML 描述的版面轉換成對應的 View 物件樹。它有四種多載形式,其中最常用的是:
View inflate(int resource, ViewGroup root)
兩個參數的意義:
resource:res/layout 下該 XML 在 R 檔案中對應的 ID,必填。root:可為 null。為 null 時只產生 resource 對應的 View;不為 null 時,產生的 View 會被加到 root 之下,並套用 root 的 LayoutParams。還有帶 attachToRoot 參數的版本。當 attachToRoot 為 true 時,inflate 完就直接掛上去了,返回值等於 root;為 false 時只回傳新建立的 View,由你決定何時掛。這是另一個常見的混淆點。
一句話講完:
setContentView()一旦呼叫,版面會立刻顯示在 Activity 上;inflate()只是把 Layout 變成一個 View 物件,有需要時再用setContentView(view)或addView()呈現。
所以在 Activity 中顯示主版面用 setContentView;而在非 Activity 環境(自訂 View、Adapter、Dialog、Fragment)要動態產生版面,就得用 LayoutInflater。
假設有一個 XML:
<TextView android:id="@+id/tview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="ATAAW.COM" /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="按鈕" />
在程式中動態載入並取得其中的元件:
LayoutInflater flater = LayoutInflater.from(this); View view = flater.inflate(R.layout.example, null); // 必須透過這個 view 去找子元件,而不是 Activity.findViewById Button button = (Button) view.findViewById(R.id.button); TextView textView = (TextView) view.findViewById(R.id.tview);
這裡有個關鍵:findViewById 要在哪個物件上呼叫,取決於那個 View 在哪棵樹裡。動態 inflate 出來的 view 尚未掛到 Activity 上,用 Activity.findViewById 會找不到,甚至出現空指標。
兩種方式:
// a. 從 SystemService 取
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// b. 從 Context 靜態方法取
LayoutInflater inflater = LayoutInflater.from(context);
看原始碼就知道兩者完全等價,from() 內部就是呼叫 getSystemService:
/**
* Obtains the LayoutInflater from the given context.
*/
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
差別只在 from() 多了一層 null 檢查,程式碼比較短,日常開發用 b 就好。
兩者經常被拿來比較,其實不是同層次的東西:
這兩個也常搞混:
R.layout.xx 參照的是 res/layout/xx.xml 這個版面檔,交給 inflate 使用。R.id.xx 參照的是版面檔中某個元件的 id,交給 findViewById 使用。所有元件的 id 都能用 R.id.xx 查到,但前提是那個元件所在的版面已經被載入。若元件不在 setContentView 載入的版面中,Activity.findViewById() 就會找不到它。
實務上更常見的不是整個換掉 Activity 版面,而是把 inflate 出來的 View 塞進某個容器:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.placeslist_linearlayout); linearLayout.addView(place_type_text);
inflate(res, parent, false)——傳了 parent 讓系統取得 LayoutParams,但 attachToRoot 設 false 避免立刻掛上去造成父子重複。
總結:inflate 是「生產」,setContentView 是「上架」。生產不一定上架,上架的通常是生產出來的東西。理解這個關係,動態 UI 的各種場景就不會亂了。
Android 斷點下載實作教學:用 SQLite 記錄進度並支援多執行緒續傳
近期專案裡需要實作斷點下載——同一個檔案可以分多次下載、中途可以暫停,之後再從上次的位置接著跑。這是大多數具備離線快取功能的 App 都必須具備的能力。本文以 SQLit
Android 自動掛斷電話的實作:透過 AIDL 呼叫 ITelephony
透過前面《AIDL 小結》與《AIDL 深入》兩篇文章,相信各位對 Android 的 AIDL 機制已經有了基本認識。這次我們就來動手實作一個實用的功能——自動掛斷電話
Android 測試入門:系統架構、設計特點與 Linux 檔案系統基礎
要做 Android 測試,第一步是理解被測對象的結構。本文從系統架構談起,接著說明 Android 的設計特點,最後補上必要的 Linux 檔案系統知識。 一、And
Android 從 init 到開機動畫的啟動流程簡析
這裡簡單記錄一下 Android 從 init 啟動到開機動畫顯示、再到桌面出現的整體流程,並標出 SurfaceFlinger 的啟動位置。 整體流程 init 主進