一、LayoutInflater的作用
将一个布局文件XML实例化为一个View对象;
二、常见用法:
//用法一
View view= LayoutInflater.from(this).inflate(R.layout.text_view,linearLayout);
//用法二
View view= LayoutInflater.from(this).inflate(R.layout.text_view,linearLayout,true);
//用法三
View view= LayoutInflater.from(this).inflate(R.layout.text_view,linearLayout,false);
2.1先看两个参数的inflate()方法:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
第一个参数:资源文件xml的id
第二个参数,根view,它可以为null,它内部调用的是三个参数的inflate()方法
inflate(resource, root, root != null);
当root=null的时候,相当于调用
inflate( resource, null, false);
当root不为空的时候,相当于调用
inflate( resource, root, true);
2.2含三个参数的inflate()方法;
明白了带有三个参数的inflate方法,含两个参数的inflate方法自然就会用了;
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
...........}
第一个参数:资源文件xml的id
第二个参数,可选的根view,也就是说它可以为null;
第三个参数,生成的view对象是否作为第二个参数root的一个子view;attachToRoot为true,则是;attachToRoot为false,则不是。当attachToRoot为false时,第二个参数root存在的意义是,保证第一个参数的资源文件中的根view的LayoutParams(Layout_width和layout_height)正确显示;
三、测试代码示例
3.1
activity_main.xml
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="我是一个帅哥"
android:gravity="center"
android:background="#00ff00"/>
运行下面代码:
setContentView(R.layout.activity_main);
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.activity_main);
RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.rl_container);
View view= LayoutInflater.from(this).inflate(R.layout.text_view,linearLayout);
和下面代码
setContentView(R.layout.activity_main);
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.activity_main);
RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.rl_container);
View view= LayoutInflater.from(this).inflate(R.layout.text_view,linearLayout,true);
效果图:
textView的高度150dp正确显示,上面的两个代码表示的是同一个意思;
这个时候填充的view,它有一个父view,就是第二个参数root,这个时候你如果在:
linearLayout.addView(view);
会报错:
java.lang.IllegalStateException: The specified child already has a parent.
3.2
第三个参数设置为false
setContentView(R.layout.activity_main);
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.activity_main);
RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.rl_container);
View view= LayoutInflater.from(this).inflate(R.layout.text_view,linearLayout,false);
linearLayout.addView(view);
效果图:
3.3
setContentView(R.layout.activity_main);
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.activity_main);
RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.rl_container);
View view= LayoutInflater.from(this).inflate(R.layout.text_view,null);
linearLayout.addView(view);
和代码:
setContentView(R.layout.activity_main);
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.activity_main);
RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.rl_container);
View view= LayoutInflater.from(this).inflate(R.layout.text_view,null,false);
linearLayout.addView(view);
效果图:
总结下:
inflate( resource, root, attachToRoot);
当root不为null,且attachToRoot为true时,调用inflate()方法,把xml中的资源文件实例化为view对象,并将root作为这个view的父view(即这个view对象不能再作为子view添加到其他ViewGroup中)
当root不为null,且attachToRoot为false时,调用inflate()方法,
xml实例化为view对象,这个view对象的根view的layoutParams会正确测量显示;
当root为null时,调用inflate()方法,xml实例化view,这个view对象的根view的layoutParams默认为LayoutParams.match_parent, LayoutParams.wrap_content;