使用LayoutInflater的理解
今天封装xml布局到view的时候遇到了内容直接变成包裹内容,显示与写的布局不符合的情况,检查后发现是LayoutInflater使用错误的问题,因此这里今天就记录一下LayoutInflater的部分理解。 LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;类似findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等),并且实例化。 获得 LayoutInflater 实例的三种方式 1.LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater() 2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); 3.LayoutInflater inflater = LayoutInflater.from(context); 研究源码可以看到 getLayoutInflater() 中调用了 LayoutInflater.from(context), 而LayoutInflater.from(context) 中又调用了(LayoutInflater)context.getSystemService 所以可以知道,三种调用方式本质是没有区别的。 造成我的布局问题的就是下边LayoutInflater的inflate方法, inflater.inflate(R.layout.item_list,null); —-> 显示布局变成默认包裹内容 inflater.inflate(R.layout.item_list,parent,false);—–> 正常显示布局 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |