LayoutInflater.inflate
方法应该是Android程序员最常使用的方法之一了,但是如果使用不当,你会碰见很多的坑。。。今天我就碰到了一个,我找到了解决方法,也打算把它记下来。。。
事情是这样的,我有一个LineaLayout
,然后在代码中会inflate
若干个View
添加到这个LineaLayout
中,但是坑出现了。。。
0x00 一个栗子
需要添加到LinearLayout中的View布局文件类似如下():
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="@android:color/holo_green_dark"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_media_menu_icon"
android:layout_height="24dp"
android:layout_width="24dp"
android:src="@drawable/ic_mv"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/tv_media_menu_text"
android:text="bxbxbai"
style="@style/Menu_TextView"/>
</RelativeLayout>
这个View
的宽固定为80dp
,高和父容器一样,然后就是inflate
这个View并且添加到这个LinearLayout
中(我故意将这个布局文件的background设置一个颜色,这样可以很清晰的看出这个View占的位置)
LinearLayout layout = (LinearLayout)findViewById(R.id.container);
View view = View.inflate(this, R.layout.layout_menu_item, null);
layout.addView(view);
这样写的话,你就会发现布局文件R.layout.layout_menu_item
中的android:layout_width="80dp"
不起作用!!也就是说View.inflate
方法忽略了布局文件的宽度设置
可是你又可以发现View.inflate
方法中还有第三个参数ViewGroup root
,Android文档中是这么写的:
A view group will be the parent. Used to properly inflate the layout_* parameters
那么可以猜想肯定和这个参数有关,下面改写代码:
LinearLayout layout = (LinearLayout)findViewById(R.id.container);
View view = View.inflate(this, R.layout.layout_menu_item, layout);
layout.addView(view);
你就会发现这样写会崩溃!然后下面这样写就没问题了:
LinearLayout layout = (LinearLayout)findViewById(R.id.container);
View view = View.inflate(this, R.layout.layout_menu_item, layout);
View.inflate
方法自动将生成的View添加到了这个ViewGroup root
中去了!!
你可以inflate
多个View,就可以看到下面这样样子了:
0x01 inflate详解
其实View.inflate
方法是调用了LayoutInflater.from(context).inflate(resource, root, root != null)
方法,而inflate方法的三个参数如下:
- resource: 布局文件的id,比如
R.layout.layout_menu_item
- root:这是一个可选参数,resource布局文件中
layout_*
参数设置的参照物就是这个root,也就是说inflate
方法会根据这个root的大小,将resource布局文件中layout_*
参数转换成一个LayoutParam
对象 - attachToRoot:是否将这个生成的View添加到这个root中去
inflate
方法会返回resource布局文件产生的View
上面栗子中调用了View.inflate(Context context, int resource, ViewGroup root)
,这个方法本质上是调用了了LayoutInflater.from(context).inflate(resource, root, root != null)
,在这个inflate
方法中可以找到下面代码:
// We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {
root.addView(temp, params);
}
可见inflate
方法自动将这个生成的View
添加到了这个root
中去了
0x02 实验
因为可以调用inflate
方法的途径有很多,下面就来做实验总结一下:
实验0
布局文件R.layout.layout_menu_item
中android:layout_height="match_parent"
改为android:layout_height="10dp"
LinearLayout layout = (LinearLayout)findViewById(R.id.container);
View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, null);
layout.addView(v1);
// 结果: layout_height = match_parent layout_width = match_parent
实验1
布局文件R.layout.layout_menu_item
中android:layout_height="match_parent"
改为android:layout_height="10dp"
LinearLayout layout = (LinearLayout)findViewById(R.id.container);
View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, null);
layout.addView(v1, 200, 200);
// 结果: layout_height = 200 layout_width = 200
实验2
布局文件R.layout.layout_menu_item
中android:layout_height
值改为match_parent
LinearLayout layout = (LinearLayout)findViewById(R.id.container);
View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, layout, false);
layout.addView(v1);
// 结果: layout_height = match_parent layout_width = 80dp
// v1 = RelativeLayout 因为 attachRoot = false
实验3
布局文件R.layout.layout_menu_item
中android:layout_height
值改为match_parent
LinearLayout layout = (LinearLayout)findViewById(R.id.container);
View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, layout, true);
//layout.addView(v1);
// 结果: layout_height = match_parent layout_width = 80dp
// 不需要layout.addView, 因为设置attachRoot=true, 生成的View自动添加到root中去了
// v1 = root 因为 attachRoot = true
0x03 源码分析
|
|
实验3的原因在inflate详解中已经介绍过了。
总之原因就在上面代码71-74
行,布局文件R.layout.layout_menu_item
生成的View会因为attachToRoot
参数为true
,就将这个生成的View
添加到root
中去,然后inflate
方法会返回这个root
的View
看上面代码54-60
行,如果root不为null的话,就会为这个布局文件R.layout.layout_menu_item
生成一个LayoutParam
对象,如果attachToRoot
参数为false
,那么就将这个param
对象给这个布局文件的View(看55行)。如果attachToRoot
参数为true
,那么就在上面代码第70行,将这个布局文件的View
和param
参数添加到root
中。
0x04 总结
-
调用
LayoutInflater.inflate
方法,并且将root参数设置为null
,就等于忽略了xml布局文件中的layout_×
参数 -
如果
root
不为null
,并且attachRoot=true
,那么就会根据root
生成一个布局文件View
的LayoutParam
对象,并且将这个View
添加到root
中去,并且返回这个root
的View
-
因此,最好还是使用这个代码吧:
View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, layout, false);