(转)View.inflate和LayoutInflater的inflate方法区别

来源:互联网 发布:创维网络电视价格 编辑:程序博客网 时间:2024/05/20 23:58

原地址:http://blog.csdn.net/amo_te_ama_me/article/details/51055283


平时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);


0 0
原创粉丝点击