LayoutInflater的參數

LayoutInflater的參數

  1. 如果root爲null, attachToRoot將失去作用,設置任何值都沒有意義
  2. 如果root不爲null, attachToRoot設爲true, 則會給加載的佈局文件指定一個父佈局,即root
  3. 如果root不爲null, attachToRoot設爲false, 則會將佈局文件最外層的所有layout屬性進行設置,當該view被添加到父view當中時,這些layout屬性會自動生效
  4. 在不設置attachToRoot參數的情況下,如果root不爲null, attachToRoot參數默認爲true

實踐出真知, 寫個例子看看:

工程包含兩個佈局文件
layout/activity_attach_to_root.xml包含4個button和用於顯示Inflater的root

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.silion.androidproject.attachtoroot.AttachToRootActivity">

    <Button
        android:id="@+id/btTrue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="true"
        android:textSize="25sp"/>

    <Button
        android:id="@+id/btFalse"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="false"
        android:textSize="25sp"/>

    <Button
        android:id="@+id/btNull"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="null"
        android:textSize="25sp"/>

    <Button
        android:id="@+id/btAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add"
        android:textSize="25sp"/>

    <LinearLayout
        android:id="@+id/llContainer"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="vertical"/>

    <LinearLayout
        android:id="@+id/llAdd"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical"/>
</LinearLayout>

layout/inflater_attach_to_root.xml被infalter的view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:text="InflaterView"
        android:textSize="20sp"
        android:gravity="center"
        android:background="#8000"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

AttachToRootActivity.java

package com.silion.androidproject.attachtoroot;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.silion.androidproject.R;

public class AttachToRootActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout mllContainer;
    private LinearLayout mllAdd;
    private View mInflaterView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attach_to_root);
        mllContainer = (LinearLayout) findViewById(R.id.llContainer);
        mllAdd = (LinearLayout) findViewById(R.id.llAdd);
        findViewById(R.id.btTrue).setOnClickListener(this);
        findViewById(R.id.btFalse).setOnClickListener(this);
        findViewById(R.id.btAdd).setOnClickListener(this);
        findViewById(R.id.btNull).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            /**
             * root = mllContainer
             * attachToRoot = true
             */
            case R.id.btTrue: {
                mllContainer.removeAllViews();
                mInflaterView = LayoutInflater.from(this).inflate(R.layout.inflater_attach_to_root, mllContainer, true);
                break;
            }
            /**
             * root = mllContainer
             * attachToRoot = false
             */
            case R.id.btFalse: {
                mllContainer.removeAllViews();
                mInflaterView = LayoutInflater.from(this).inflate(R.layout.inflater_attach_to_root, mllContainer, false);
                break;
            }
            /**
             * root = null
             * attachToRoot invail
             */
            case R.id.btNull: {
                mllContainer.removeAllViews();
                mInflaterView = LayoutInflater.from(this).inflate(R.layout.inflater_attach_to_root, null, false);
                break;
            }
            case R.id.btAdd: {
                mllAdd.removeAllViews();
                if (mInflaterView != null) {
                    mllAdd.addView(mInflaterView);
                }
            }
            default:
                break;
        }
    }
}

界面:
這裏寫圖片描述

  • root不爲null, attachToRoot設爲true
    點擊TRUE, inflater view並且添加到root顯示

    這裏寫圖片描述

    再點擊ADD手動添加, 出現FC

    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  • root不爲null, attachToRoot設爲false
    點擊FALSE, inflater view但沒有添加到root顯示

    這裏寫圖片描述

    再點擊ADD手動添加, 保持xml設置的LayoutParams
    這裏寫圖片描述

  • root爲null, attachToRoot不起作用
    點擊FALSE, inflater view但沒有添加到root顯示
    這裏寫圖片描述
    再點擊ADD手動添加, 出現了奇怪的變化
    這裏寫圖片描述

結論 : 避開崩潰、異常表現與誤解
如果可以傳入ViewGroup作爲根元素,那就傳入它。
避免將null作爲根ViewGroup傳入。
當我們不負責將layout文件的View添加進ViewGroup時設置attachToRoot參數爲false。
不要在View已經被添加進ViewGroup時傳入true。

發佈了41 篇原創文章 · 獲贊 8 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章