关于LayoutInflater的总结
来源:互联网 发布:股票交易系统测试软件 编辑:程序博客网 时间:2024/05/15 16:37
首先先看一下Android Studio Lint的警告:
Avoid passing null
as the view root (needed to resolve layout parameters on the inflated layout's root element) less... (Ctrl+F1)
When inflating a layout, avoid passing in null as the parent view, since otherwise any layout parameters on the root of the inflated layout will be ignored.
所在代码是这样的:headerView= LayoutInflater.from(getContext()).inflate(R.layout.home_title,null);
这段警告意思很明了:如果第二个参数传null,那么layout的根部局的所有属性都被忽略。
inflate的几个重载函数:
1.inflate(@LayoutRes int resource, @Nullable ViewGroup root)
2.inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
3.inflate(XmlPullParser parser, @Nullable ViewGroup root)
可知,最后调用的还是三个参数的方法inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)4.inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)
常用的是前两种方式,通过源码:public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) { return inflate(resource, root, root != null);}
那么我们常用的就四种组合
1.inflate(R.layout.item,null,false); 通过源码可知这种形式相当于inflate(R.layout.item, null)
root为空,会导致R.layout.item的根布局失效,如果用在ListView或者RecyclerView的布局中会为达到效果,需要内嵌一个布局,这样就造成了根布局的冗余;
2.inflate(R.layout.item,null,true);
不管true或者false,都要手动添加布局到root上。
3.inflate(R.layout.item,root,false);
item的根布局有效,因为attachToRoot为false,需要手动添加item到root上。
4.inflate(R.layout.item,root,true);
item的根布局有效,因为attachToRoot为true,inflate会自动添加item到root上。
0 0
- 关于LayoutInflater的总结
- 关于LayoutInflater的一些总结
- 关于LayoutInflater的用法
- LayoutInflater的用法总结
- 关于LayoutInflater的错误用法
- 关于LayoutInflater的一些梗
- 关于Android LayoutInflater的用法
- 关于LayoutInflater的错误用法
- 关于LayoutInflater的方法讲解
- LayoutInflater.inflate的用法总结
- 关于LayoutInflater
- 关于layoutInflater
- 关于LayoutInflater
- 关于LayoutInflater
- 关于LayoutInflater
- 关于LayoutInflater
- 关于LayoutInflater
- 关于LayoutInflater
- C#实现office文档转换为PDF或xps的一些方法
- 357. Count Numbers with Unique Digits
- 多线程控件讲解
- Servlet学习笔记二
- HTTP 协议入门
- 关于LayoutInflater的总结
- 窗体泄露 android.view.WindowLeaked: Activity XxxActivity has leaked window android.widget.PopupWindow$Po
- Java 数组转List
- 数据存储,实现用户登录界面及记住密码的实现。
- netfilter bug scheduling while atomic: swapper/0/0x10000100
- 2016 版 Laravel 系列入门教程(四)【最适合中国人的 Laravel 教程】
- Android Studio下开发JNI第一步
- 更新Cocoapods时
- 蓝桥杯 四平方和