LayoutInflater的参数
- 如果root为null, attachToRoot将失去作用,设置任何值都没有意义
- 如果root不为null, attachToRoot设为true, 则会给加载的布局文件指定一个父布局,即root
- 如果root不为null, attachToRoot设为false, 则会将布局文件最外层的所有layout属性进行设置,当该view被添加到父view当中时,这些layout属性会自动生效
- 在不设置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。