View.inflate和LayoutInflater的inflate方法区别

平时ListView加载item中,adapter的getView方法中,我们经常用到:

LayoutInflater.from(mContext).inflate(R.layout.it
,parent,false);

这样的方法来加载布局xml,平时一直就是这么用的,也没什么疑问。今天网上看了个自定义布局的源码,自定义布局中加载布局xml用的View.inflate方法:

public class SettingItemView extends RelativeLayout {

    private CheckBox cb_status;
    private TextView tv_description;
    private TextView tv_title;

    private String desc_on;
    private String desc_off;

    private void iniView(Context context) {
        View.inflate(context, R.layout.setting_item_view, this);//第三个参数传布局文件的父类
        cb_status=(CheckBox) this.findViewById(R.id.cb_status);
        tv_description=(TextView) this.findViewById(R.id.tv_description);
        tv_title=(TextView) this.findViewById(R.id.tv_title);
    }

第一次见用这种方式来加载布局的,看了下他的listview加载item,也是用这种方式:

@Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder holder;
            if(convertView==null){
                view=View.inflate(getApplicationContext(), R.layout.list_item_callsms, null);//最后一个传了null
                holder=new ViewHolder();
                holder.tv_number=(TextView) view.findViewById(R.id.tv_black_number);
                holder.tv_mode=(TextView) view.findViewById(R.id.tv_black_mode);
                holder.iv_delete=(ImageView) view.findViewById(R.id.iv_delete);
                view.setTag(holder);

好吧,看一下View.inflate的说明:
Open Declaration View android.view.View.inflate(Context context, int resource, ViewGroup root)

Inflate a view from an XML resource. This convenience method wraps the
LayoutInflater class, which provides a full range of options for view
inflation.

Parameters: context The Context object for your activity or
application. resource The resource ID to inflate root A view group
that will be the parent. Used to properly inflate the layout_*
parameters.
See Also: LayoutInflater
最后有一句让你看LayoutInflater这个类,怀疑它内部也是用LayoutInflater实现的,进入源码:

 public static View inflate(Context context, int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
    }

果然内部也是用LayoutInflater实现的,不知道为啥android还要用View.inflat封装一下。。。o(〃’▽’〃)o
其中LayoutInflater的Inflate的三个参数意思为:

对于Inflate的三个参数(int resource, ViewGroup root, boolean attachToRoot)

如果inflate(layoutId, null )则layoutId的最外层的控件的宽高是没有效果的

如果inflate(layoutId, root, false ) 则认为和上面效果是一样的

如果inflate(layoutId, root, true ) 则认为这样的话layoutId的最外层控件的宽高才能正常显示

对这三个参数区别不理解的话可以看这篇文章:
inflate第三个参数意思

从源码角度解析的有郭大神的:
Android LayoutInflater原理分析,带你一步步深入了解View(一)
以及另一篇感觉很不错的:
Android LayoutInflate深度解析 给你带来全新的认识

看完,你应该知道这个参数意思了,ok,再来看上面代码, 这时就可以替换为layoutInflater的方式了:

对于第一个自定义布局:

//View.inflate(context, R.layout.setting_item_view, this);//第三个参数传布局文件的父类
        LayoutInflater.from(context).inflate(R.layout.setting_item_view, this, true);//等价于上面

第二个适配器中getView:

//view=View.inflate(getApplicationContext(), R.layout.list_item_callsms, null);
                view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item_callsms,parent,false);
java.lang.IllegalStateException: ScrollView can host only one direct child at androidx.core.widget.NestedScrollView.addView(NestedScrollView.java:507) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1131) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1130) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) at android.view.LayoutInflater.inflate(LayoutInflater.java:686) at android.view.LayoutInflater.inflate(LayoutInflater.java:505) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:360) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:443) at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:722) at com.android.tools.idea.rendering.RenderTask.lambda$inflate$9(RenderTask.java:879) at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$3.run(RenderExecutor.kt:194) at com.android.tools.idea.rendering.RenderExecutor$PriorityRunnable.run(RenderExecutor.kt:292) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:829)
06-06
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值