Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android LayoutInflater 用法詳解:inflate 的三個參數與 attachToRoot 的真正差異

編輯:關於 Android 程式設計

LayoutInflater 的作用只有一句話:把一個 XML 版面配置檔具現化為 View 物件。聽起來簡單,但 inflate() 的第二、第三個參數到底怎麼搭配,是很多人一直沒搞清楚的地方。

一、取得 LayoutInflater 物件

網路上流傳三種寫法,但追一下原始碼就會發現本質上都是呼叫 context.getSystemService(),所以直接寫成下面這一句最清楚:

LayoutInflater inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

二、inflate() 的回傳值

回傳一個 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 底下:

  • root 不為 null 且 attachToRoot 為 true:建立後立刻 addView 到 root。
  • root 不為 null 且 attachToRoot 為 false:不掛入,但會用 root 的 generateLayoutParams() 產生正確的 LayoutParams 套用到新 View 上。
  • root 為 null:attachToRoot 必須為 false,且 XML 根節點的 layout_width/height 會失效(等同 wrap_content 的推論結果)。

四、用實例驗證

版面一:activity_main.xml

<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>

版面二:inflatertest.xml

<?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>

寫法 A:兩參數版本

@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);
}

寫法 B:attachToRoot 為 true

@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 裡:

寫法 C:attachToRoot 為 false

@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。
  • 在自訂 Adapter 的 getView() 中,正確寫法是 inflate(R.layout.item, parent, false):ListView/RecyclerView 會自行把 View 掛上去,若傳 true 會導致 "child already has a parent" 這類錯誤。
  • 在 Fragment 的 onCreateView() 中也是傳 false,並自行回傳 inflate 的結果。
  • 學框架一定要把「回傳值」與「每個參數的意義」都弄清楚,並養成歸納規律的習慣,這比死記 API 有用得多。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved