LayoutInflater是Android开发者接触最多的一个类之一,主要作用是用来解析layout资源文件,将其实例化成一个View对象。LayoutInflater的API非常非常简单,对开发者来说往往只要一行代码,然而就是那一行代码能正确使用的人却寥寥无几。
话不多说,先上代码:
View view = getLayoutInflater().inflate(R.layout.activity_main, null);
上面的代码在Android项目中几乎无处不在,很少有人意识到这样可能已经造成了布局冗余。
好在现在的编辑器Android Studio或Eclipse足够智能,这种方式调用时会出现警告提示:
Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element).
When inflating a layout, avoid passing in null as the parent view, since otherwise any layout parameters on the root of the inflated layout will be ignored.
这段警告意思很明了:如果第二个参数传null,那么layout的根部局的所有layout类型属性都被忽略(layout_xxx),包括width、height、margin等等。
如果还意识不到这个问题有多严重,那么我们来看具体场景。
ListView、GridView的Item属性无效问题
几乎每一个使用过ListView或GridView的开发者,都曾遇到过这样的问题,Item根布局的layout属性失效,正常的代码如下:
item_list
<?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="100dip" />
Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_list, null);
}
...
return null;
}
乍一看没什么毛病,运行到手机上之后问题来了,item的高度100dip无效,然后……所谓解决方案来了:
修正后item_list
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dip">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dip" />
</FrameLayout>
运行到手机上之后,完美!然后……总结下方案:ListView的Item布局必须在最外层再加一层布局,GridView同之。
但是,但是,但是……
很明显,出现了布局冗余,如果按照一屏幕显示10个Item来算的话,一下子就冗余了11个FrameLayout布局。
在寻求问题的真正解决之道前,我们先来分析一下问题产生原因。
来看LayoutInflater的inflate方法的源码:
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
...
// Temp is the root view that was found in the xml
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
...
// Decide whether to return the root that was passed in or the top view found in xml.
if (root == null || !attachToRoot) {
result = temp;
}
...
return result;
}
只有当root不为null时才会generateLayoutParams,这么设计的理由很简单,因为如果不清楚容器布局的类型和属性,是无法确定子布局的LayoutParams类型的。比如如果父布局是FrameLayout,那么子布局的LayoutParams必须是FrameLayout.LayoutParams而不能是LinearLayout.LayoutParams,不然属性就全乱套了。
所以,如果需要使得ListView或GridView的Item布局的LayoutParams属性生效,那么这个root父容器必须有效,所以Adapter的getView方法正确书写方式如下:
修正后Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_list, parent, false);
}
...
return null;
}
正确解决方案:inflate方法的第二个参数传parent,第三个参数传false
这也恰恰是BaseAdapter的getView方法为什么会提供一个parent参数的真正原因!至于第三个参数的意义是表示这个布局不会被立刻添加到parent中。
其实,不光光是BaseAdapter中需要正确使用LayoutInflater,其它地方也是一样的,如果没有正确使用,那么百分之七八十出现了布局冗余,而这些都是可以优化的。
另外,在公用控件设计方面。如果一个可扩展或待实现的方法返回的是一个View,那么尽量开放一个ViewGroup父布局对象,这样才能够避免子布局出现布局冗余的问题。
A方式
public abstract View getView(ViewGroup parent);
B方式
public abstract View getView();
很明显,A方式设计得要比B方式更加合理更加健壮!
本博客不定期持续更新,欢迎关注和交流: