Android LayoutInflater

本文讲述什么是 LayoutInflater,有何作用,使用场景,以及与 setContentView() 的区别。应用案例请参见我的另一篇博文《Android PopupWindow 仿微信点赞和评论弹出框》

1. LayoutInflater 是什么

LayoutInflater 是一个抽象类(abstract class),继承 Object 。

1.1 官方定义

以下翻译自 Android 官方文档对 LayoutInflater 的 Class Overview 描述:
把 res/layout/ 中的布局文件实例化成对应的 View 对象。不能直接使用,而要通过 getLayoutInflater() 或 getSystemService(Class) 得到,通过这两个方法得到的 LayoutInflater 对象才是绑定到当前 Context 而且在当前硬件设备上正确配置好的。
例如:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

如果你想为你自己的View创建另一个LayoutInflater,可以使用LayoutInflater.Factory。首先调用cloneInContext(Context)函数来复制一个已经存在的ViewFactory,然后再调用setFactory(LayoutInflater.Factory)方法。

由于性能原因,view扩展极大地依赖于在编译期间对XML文件的预处理。因此,现在还不能在运行时通过XmlPullParser来使用LayoutInflater。它仅仅只能使用XmlPullParser中返回的已经编译过的资源文件(R.something 索引的文件文件)。

1.2 通俗定义

将 res/layout/ 中的布局文件,转成对应的 View 对象,以便对该 View 对象进行后续操作,如添加数据、更改属性、添加父或子View 等。


2. LayoutInflater 有何作用

一个字,动态加载 layout。动态加载是指,在应用运行过程中进行加载。


这里写图片描述


3. LayoutInflater 在何时使用

在业务逻辑中,我们总要对各种 View 对象(如TextView,ImageView,ListView等)做各种操作(如更改属性)。而操作这些 View 对象的前提是 View 所在的 layout 文件已经被加载,而加载的方式有两种:

  • Activity.setContentView(R.layout.some_layout),这是比较常见的一种方式,一般在重写的 onCreate() 方法中完成;
  • LayoutInflater.inflate(R.layout.some_layout),该方法返回一个 View 对象,即 some_layout 对象的 Layout 对象或某个具体的 View 对象(TextView,ImageView,ListView);

待 some_layout (注意:some_layout.xml 中可以只有一个 View 控件,也可以是一个像 LinearLayout 或 RelativeLayout 之类的 Layout)加载完毕,就生成了一棵视图树,对于视图树中的 View,我们可以通过 findViewById() 获得,然后进行各种后续操作。

例如我们熟知的微信朋友圈点赞功能(详见我的另一篇博文《Android PopupWindow 仿微信点赞和评论弹出框》),点击后弹出的那个窗口(PopupWindow)就适合使用inflate()填充layout:


这里写图片描述


4. LayoutInflater 如何使用

有3种方式获得 LayoutInflater 实例:

LayoutInflater mInflater = getLayoutInflater();
LayoutInflater mInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater mInflater = LayoutInflater.from(context);  

推荐使用第二种,查看源码可知其他两种最终也是调用第二种方式。
获得实例后,加载 layout 文件:

View view = mInflater.inflate(R.layout.some_layout, parent, false);

其中,view 可能是一个 View(TextView,ImageView,ListView 等),也可能是一个 Layout (如 LinearLayout 或 RelativeLayout 等)。对应的,则有如下操作:

TextView tv = (TextView) mInflater.inflate(R.layout.some_layout, null, false);

View rootView = mInflater.inflate(R.layout.some_layout, null, false);
TextView tv = rootView.findViewById(R.id.tv);

下面是一个摘自 stackoverflow 上关于 listview 的例子:

list_layout.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" >
    <TextView 
        android:id="@+id/field1"
        android:layout_width="0dp"  
        android:layout_height="wrap_content" 
        android:layout_weight="2"/>
    <TextView 
        android:id="@+id/field2"
        android:layout_width="0dp"  
        android:layout_height="wrap_content" 
        android:layout_weight="1"
/>
</LinearLayout>

schedule_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"  
    android:layout_height="wrap_content" 
    android:layout_weight="1"/>

重写 Adapter 的 getView() 方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = activity.getLayoutInflater();
    View lst_item_view = inflater.inflate(R.layout.list_layout, null);
    TextView t1 = (TextView) lst_item_view.findViewById(R.id.field1);
    TextView t2 = (TextView) lst_item_view.findViewById(R.id.field2);
    t1.setText("some value");
    t2.setText("another value");

    // dinamically add TextViews for each item in ArrayList list_schedule
    for(int i = 0; i < list_schedule.size(); i++){
        View schedule_view = inflater.inflate(R.layout.schedule_layout, (ViewGroup) lst_item_view, false);
        ((TextView)schedule_view).setText(list_schedule.get(i));
        ((ViewGroup) lst_item_view).addView(schedule_view);
    }
    return lst_item_view;
}

上述程序中,先加载 listview 每行的布局文件 schedule_layout.xml,然后动态的向每行布局文件中动态的加入一个 schedule_view,从而完成 listview 每行的布局和数据填充。


5. 关于参数

5.1 layout 根节点为 merge

在 inflate 以 merge 为根节点的 layout 时,attachToRoot 必须要为 true,否则会报错:FATAL EXCEPTION: main android.view.InflateException: merge can be used only with a valid ViewGroup root and attachToRoot=true;

5.2 root 参数不要为 null

root 不要为 null,具体原因见《Layout Inflation as Intended》
而且,如果 root = null,inflate 进来的 view 的 LayoutParams 是 null,即 view 的 width、height、margin 等全部失效。

以下规则都是在 root 不为 null 的前提下。

inflate 方法有中形式:
View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

   public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
        return inflate(resource, root, root != null);
    }

第二种形式中,attachToRoot 的值取决于 root 参数是否为 null。

当 attachToRoot = true 时该 view 会被添加到 root,故不能再显式调用 addView() 否则抛异常。如果想通过 addView() 将 inflate 进来的 view 加到 root,则必须要将 attachToRoot 设置为 false;


6. 与 Activity.setContentView 的区别

setContentView() 方法则隶属于 Activity,而 LayoutInflater.inflate() 则没有此限制。


7. 参考

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值