Android LayoutInflater深度解析

前言:玩了这么长时间的安卓,相信大家一定对LayoutInflater不陌生,特别是在使用Adapter的时候会经常使用的LayoutInflater来获取要显示的Item,下面我们就来玩一下:

一、LayoutInflater的inflate方法
首先我们先来看一下 inflate 这个方法的参数的两个重载的方法:
1、inflate(int layoutID, ViewGroup root)
2、inflate(int layoutID, ViewGroup root,boolean attachToRoot )
那么问题来了,我们在使用时应该怎么选择呢?
  • 使用方式一:inflate(layoutID,null) 注意:这里root只能传递null作为参数,具体原因,我们在后面的源码中会详细讲解
  • 使用方式二:inflate(layoutID,null,false/true)
  • 使用方式三:inflate(layoutID,root,false/true)注意这里,当attachToRoot为 true 时,会报错,具体是什么原因,我们后面讲
下面我们就用代码来证实一下,我会先把代码全部贴出来,然后后面会一一讲解:
  • Activity的布局文件
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv_show"></ListView>
  • ListView的item布局文件
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/id_btn"
    android:layout_width="120dp"
    android:layout_height="120dp" >
</Button>
  • 主Activity
public class LayoutInflater_inflate extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layoutinflater_inflate);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        ListView lv_show = (ListView) findViewById(R.id.lv_show);
        ArrayList<String> lists = new ArrayList<>();
        lists.add("Android");
        lists.add("IOS");
        lists.add("WindowsPhone");
        lv_show.setAdapter(new MyAdapter(this, lists));

    }

    class MyAdapter extends BaseAdapter {
        private ArrayList<String> lists;
        private Context mContext;

        public MyAdapter(Context context, ArrayList<String> lists) {
            this.mContext = context;
            this.lists = lists;
        }

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            return lists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_layoutflater_inflate, null);
//            View view = LayoutInflater.from(mContext).inflate(R.layout.item_layoutflater_inflate, null, false);
//            View view = LayoutInflater.from(mContext).inflate(R.layout.item_layoutflater_inflate, null, true);
//            View view =LayoutInflater.from(mContext).inflate(R.layout.item_layoutflater_inflate, parent, true);
//           View view =LayoutInflater.from(mContext).inflate(R.layout.item_layoutflater_inflate, parent, false);
            Button id_btn = (Button) view.findViewById(R.id.id_btn);
            id_btn.setText(lists.get(position));
            return view;
        }
    }
}

代码相当简单,这里就不做具体详细介绍,我们来看一下使用LayoutInflater不同参数下的inflate方法的各种效果:

  • inflate(layoutID,null)
  • inflate(layoutID,null,false/true)
    效果显示如下:
    这里写图片描述

  • inflate(layoutID,root,false)
    效果显示如下:
    这里写图片描述

  • inflate(layoutID, root, true)
    效果显示如下:

 java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

你会发现报错了,报错信息为: addView(View, LayoutParams) is not supported in AdapterView 具体的错误原因我们会在源码解析的时候详细说

由此可见:

通过上面的一个例子,我们已经发现使用 inflate(layoutID,null)、inflate(layoutID,null, false/true) 这两个函数的时候,我们要加载的Item并不能正常显示我们设置的宽高值,前面我们为Item设置的宽高值为:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/id_btn"
    android:layout_width="120dp"
    android:layout_height="120dp" >
</Button>

而当我们使用inflate(layoutID, root, false)时,却能正常显示我们设置的宽高值,而inflate(layoutID, root, true)却会报错

好了,会发生什么效果我们已经知道了,下面我们就来扒扒源码吧

inflate的两个重载的方法其实底层执行的都是下面的代码,只不过inflate(layoutID, root) 走的是重载,来看源码:

    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        final Resources res = getContext().getResources();
        if (DEBUG) {
            Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
                    + Integer.toHexString(resource) + ")");
        }

        final XmlResourceParser parser = res.getLayout(resource);
        try {
        //其实最后走的都是这个方法
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }

inflate(parser, root, attachToRoot)

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");

            final Context inflaterContext = mContext;
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            Context lastContext = (Context) mConstructorArgs[0];
            mConstructorArgs[0] = inflaterContext;
            View result = root;

            try {
                // Look for the root node.
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG &&
                        type != XmlPullParser.END_DOCUMENT) {
                    // Empty
                }

                if (type != XmlPullParser.START_TAG) {
                    throw new InflateException(parser.getPositionDescription()
                            + ": No start tag found!");
                }

                final String name = parser.getName();

                if (DEBUG) {
                    System.out.println("**************************");
                    System.out.println("Creating root view: "
                            + name);
                    System.out.println("**************************");
                }

                if (TAG_MERGE.equals(name)) {
                    if (root == null || !attachToRoot) {
                        throw new InflateException("<merge /> can be used only with a valid "
                                + "ViewGroup root and attachToRoot=true");
                    }

                    rInflate(parser, root, inflaterContext, attrs, false);
                } else {
                    // 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) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // 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);
                        }
                    }

                    if (DEBUG) {
                        System.out.println("-----> start inflating children");
                    }

                    // Inflate all children under temp against its context.
                    rInflateChildren(parser, temp, attrs, true);

                    if (DEBUG) {
                        System.out.println("-----> done inflating children");
                    }

                    // 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);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }
                }

            } catch (XmlPullParserException e) {
                InflateException ex = new InflateException(e.getMessage());
                ex.initCause(e);
                throw ex;
            } catch (Exception e) {
                InflateException ex = new InflateException(
                        parser.getPositionDescription()
                                + ": " + e.getMessage());
                ex.initCause(e);
                throw ex;
            } finally {
                // Don't retain static reference on context.
                mConstructorArgs[0] = lastContext;
                mConstructorArgs[1] = null;
            }

            Trace.traceEnd(Trace.TRACE_TAG_VIEW);

            return result;
        }
    }

这段代码基本上已经告诉了我们inflate不同参数下为什么会显示不同效果,我们一步一步来:

  • 首先下面贴出的代码我们根本就不需要考虑,我们只需要知道它做的是什么操作就可以了,这部分代码其实就是在使用xml解析器在解析我们要加载的Item布局文件
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");

            final Context inflaterContext = mContext;
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            Context lastContext = (Context) mConstructorArgs[0];
            mConstructorArgs[0] = inflaterContext;

            //这里使我们需要注意的地方,这里的result就是我们要返回的view
            View result = root;

            try {
                // Look for the root node.
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG &&
                        type != XmlPullParser.END_DOCUMENT) {
                    // Empty
                }

                if (type != XmlPullParser.START_TAG) {
                    throw new InflateException(parser.getPositionDescription()
                            + ": No start tag found!");
                }
//获取根节点标签的名字
                final String name = parser.getName();

                if (DEBUG) {
                    System.out.println("**************************");
                    System.out.println("Creating root view: "
                            + name);
                    System.out.println("**************************");
                }

                if (TAG_MERGE.equals(name)) {
                    if (root == null || !attachToRoot) {
                        throw new InflateException("<merge /> can be used only with a valid "
                                + "ViewGroup root and attachToRoot=true");
                    }

                    rInflate(parser, root, inflaterContext, attrs, false);
                } else {

注意了:重头戏来了,这才是我们今天这篇文章的关键:

    // 这个方法做的操作其实就是根据 name(其实就是我们要加载的布局的根标签) 创建一个 view
                    final View temp = createViewFromTag(root, name, inflaterContext, attrs);

                    ViewGroup.LayoutParams params = null;

                    if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                            //得到我们 设置的 LayoutParams
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            //如果 attachToRoot为false,设置当前要返回的view的 LayoutParams
                            temp.setLayoutParams(params);
                        }
                    }

                    if (DEBUG) {
                        System.out.println("-----> start inflating children");
                    }

                    // Inflate all children under temp against its context.
                    rInflateChildren(parser, temp, attrs, true);

                    if (DEBUG) {
                        System.out.println("-----> done inflating children");
                    }

                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.

                    //这里就是当root不为null,且attachToRoot为true时,出现 addView(View, LayoutParams) is not supported in AdapterView异常的原因
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }

                    // Decide whether to return the root that was passed in or the
//设置我们要返回的view
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }
                }

分解代码:

  View result = root;

result 就是我们要返回的view

 final String name = parser.getName();

而这个name说白了其实就是我们通过pull解析获取的当前标签的名字

final View temp = createViewFromTag(root, name, inflaterContext, attrs);

temp 是通过 name 生成的view,也就是我们要返回的view

if (root != null) {
    params = root.generateLayoutParams(attrs);
    if (!attachToRoot) {
        temp.setLayoutParams(params);
    }
}

从这块代码中我们可以看到当root不为null,attachToRoot为false时,为temp设置了LayoutParams

if (root != null && attachToRoot) {
     root.addView(temp, params);
}

当root不为null 同时 attachToRoot为true时,将temp添加到了root中,也就是listview中,但是我们需要知道的是listview其实是AdapterView的子类,而listview中并没有重写addView(View child, LayoutParams params)方法,所以当我们调用addView时,其实调用的是 AdapterView的 addView(View child, LayoutParams params)方法,我们可以去看看 AdapterView的addView方法做了什么操作

    public void addView(View child, LayoutParams params) {
        throw new UnsupportedOperationException("addView(View, LayoutParams) "
                + "is not supported in AdapterView");
    }

现在已经可以解释我们使用LayoutInflater的inflate(layoutID,root,true)时,会报错的原因

if (root == null || !attachToRoot) {
    result = temp;
}

当root不为null,或者attachToRoot为false时,为result重新赋值为temp,最后返回result

从上面的分析可以看出:

  • inflate(layoutID, null)只创建temp,而并没有为temp设置LayoutParams,返回temp
  • inflate(layoutID,root, false)创建temp,并为temp设置LayoutParams,返回temp
  • inflate(layoutID,root,true)创建temp,并将temp添加进root,返回的是root

由上面已经能够解释:

  • Inflate(resId , null )不能正确处理宽和高是因为:layout_width,layout_height是相对了父级设置的,必须与父级的LayoutParams一致。而此temp的getLayoutParams为null

  • Inflate(resId,parent,false) 可以正确处理,因为temp.setLayoutParams(params);这个params正是root.generateLayoutParams(attrs);得到的。

  • Inflate(resId , parent,true )不仅能够正确的处理,而且已经把resId这个view加入到了parent,并且返回的是parent,和以上两者返回值有绝对的区别,还记得文章前面的例子上,MyAdapter里面的getView报的错误:

进一步的解析

上面我根据源码得出的结论可能大家还是有一丝的迷惑,我再写个例子论证我们上面得出的结论:
主布局文件:

<Button xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/id_btn"  
    android:layout_width="120dp"  
    android:layout_height="120dp"  
    android:text="Button" >  
</Button>  

主Activity:

package com.example.zhy_layoutinflater;  

import android.app.ListActivity;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  

public class MainActivity extends ListActivity  
{  


    private LayoutInflater mInflater;  

    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  

        mInflater = LayoutInflater.from(this);  

        View view1 = mInflater.inflate(R.layout.activity_main, null);  
        View view2 = mInflater.inflate(R.layout.activity_main,  
                (ViewGroup)findViewById(android.R.id.content), false);  
        View view3 = mInflater.inflate(R.layout.activity_main,  
                (ViewGroup)findViewById(android.R.id.content), true);  

        Log.e("TAG", "view1 = " + view1  +" , view1.layoutParams = " + view1.getLayoutParams());  
        Log.e("TAG", "view2 = " + view2  +" , view2.layoutParams = " + view2.getLayoutParams());  
        Log.e("TAG", "view3 = " + view3  );  

    }  

}  

可以看到我们的主Activity并没有执行setContentView,仅仅执行了LayoutInflater的3个方法。
注:parent我们用的是Activity的内容区域:即android.R.id.content,是一个FrameLayout,我们在setContentView(resId)时,其实系统会自动为了包上一层FrameLayout(id=content)。

view1的layoutParams 应该为null
view2的layoutParams 应该不为null,且为FrameLayout.LayoutParams
view3为FrameLayout,且将这个button添加到Activity的内容区域了(因为R.id.content代表Actvity内容区域)

下面看一下输出结果,和Activity的展示

07-27 14:17:36.703: E/TAG(2911): view1 = android.widget.Button@429d1660 , view1.layoutParams = null  
07-27 14:17:36.703: E/TAG(2911): view2 = android.widget.Button@42a0e120 , view2.layoutParams = android.widget.FrameLayout$LayoutParams@42a0e9a0  
07-27 14:17:36.703: E/TAG(2911): view3 = android.widget.FrameLayout@42a0a240 

可见,虽然我们没有执行setContentView,但是依然可以看到绘制的控件,是因为

View view3 = mInflater.inflate(R.layout.activity_main,(ViewGroup)findViewById(android.R.id.content), true);

这个方法内部已经执行了root.addView(temp , params); 上面已经解析过了。

也可以看出:和我们的推测完全一致,到此已经完全说明了inflate3个重载的方法的区别。相信大家以后在使用时也能选择出最好的方式。不过下面准备从ViewGroup和View的角度来说一下,为啥layoutParams为null,就不能这确的处理。

相关文章:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值