編輯:關於 Android 程式設計
LayoutInflater 的作用只有一句話:把一個 XML 版面配置檔具現化為 View 物件。聽起來簡單,但 inflate() 的第二、第三個參數到底怎麼搭配,是很多人一直沒搞清楚的地方。
網路上流傳三種寫法,但追一下原始碼就會發現本質上都是呼叫 context.getSystemService(),所以直接寫成下面這一句最清楚:
LayoutInflater inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
回傳一個 View。有了它就沒問題了——不管是 setContentView(),還是在自訂 Adapter 的 getView() 裡把項目版面取出來,都靠它。
public View inflate(int resource, ViewGroup root) public View inflate(int resource, ViewGroup root, boolean attachToRoot)
第一個參數是 XML 的資源索引,例如 R.layout.activity_main。
第二個參數 root 是「可能的父容器」。這裡是最容易誤解的部分:root 並不總是代表「要把結果塞進去」,它同時還肩負著提供 LayoutParams 參照的角色。
第三個參數 attachToRoot 決定要不要真的把新 View 掛到 root 底下:
true:建立後立刻 addView 到 root。false:不掛入,但會用 root 的 generateLayoutParams() 產生正確的 LayoutParams 套用到新 View 上。null:attachToRoot 必須為 false,且 XML 根節點的 layout_width/height 會失效(等同 wrap_content 的推論結果)。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RelativeLayout
android:id="@+id/viewgroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inflatedemo" />
</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup, true);
}
寫法 A 與寫法 B 的結果完全相同,畫面如下——CheckBox 被放進了 RelativeLayout 裡:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup, false);
}
這次的結果不一樣了:inflate 確實回傳了 View,但它並沒有被掛到畫面上,所以螢幕只剩原本 activity_main 的內容:
inflate(R.layout.x, root) 等同於 attachToRoot = true,會自動掛入並回傳該 View。getView() 中,正確寫法是 inflate(R.layout.item, parent, false):ListView/RecyclerView 會自行把 View 掛上去,若傳 true 會導致 "child already has a parent" 這類錯誤。onCreateView() 中也是傳 false,並自行回傳 inflate 的結果。
Android NFC 開發入門:標籤讀寫與 Android Beam 資料傳輸
NFC(近場通訊)是行動裝置上相當實用的短距離資料交換技術。本文將帶你了解如何在 Android 平台上執行 NFC 的基本操作,重點說明如何以 NDEF 訊息的形式收發
Android 開發必讀:程序員一定要懂的重要設計理念
日前帶著自己的作品參加服務外包大賽,因抽籤順位較後而等待许久,便與前排參賽者聊起 Android 開發經驗,彼此展示作品。有朋友建議我參考官方 Design 的設計原則,
Android 透過 Socket 與伺服器通訊的完整實作(含 Demo)
Socket 是 Android 上歷史悠久、也相當可靠的通訊方式。它不像 HTTP 那樣有著「一问一答」的限制,伺服器可以主動推送資料給客戶端,因此非常適合即時通訊、長
Android 官方文件導讀:Service 完整篇
Service 是 Android 四大元件之一。它沒有使用者介面,卻能在背景默默把事情做完——下載檔案、播放音樂、寫入資料庫、與 ContentProvider 打交道