zoukankan      html  css  js  c++  java
  • 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. 參考

  • 相关阅读:
    LeetCode 123 Best Time to Buy and Sell Stock III
    直接选择排序算法汇总
    zoom:1是什么意思
    怎么去掉织梦网站首页带的index.html/index.php
    wamp apache无法启动的解决方法
    提交到svn服务器时,一直缓冲不,
    桌面上图标都不见了怎么办听语音
    如何清除网上浏览痕迹?清除缓存
    TortoiseSVN文件夹及文件图标不显示解决方法
    快捷键
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7097174.html
Copyright © 2011-2022 走看看