关于LayoutInflater的错误用法(警告提示:Avoid passing null as the view root)
项目中用LayoutInflater加载xml布局一直飘黄警告,上网搜了搜发现没有解释明白的,到是找到了一篇外国的博文,但是竟然是英文。。。幸好以前上学时候的英语不是很差加上谷歌的辅助,简单翻一下!
原文地址:http://www.cnblogs.com/kobe8/p/3859708.html
参考博客:http://blog.csdn.net/ccfcccfc/article/details/40984971
错误用法:
inflater.inflate(R.layout.my_layout, null);
加载xml布局常用的有这两个方法:
inflate(int resource, ViewGroup root)
resource:将要加载的xml布局
root:指定“将要加载的xml布局”的“根布局”
inflate(int resource, ViewGroup root, boolean attachToRoot)
attachToRoot:是否将解析xml生成的view加载到“根布局”中
注意:这里的参数root的设置很关键,会影响到xml解析生成的view;如果设置成null即没有指定“根布局”的话,xml的最外层根布局设置的Android:layout_xxx等属性不会生效,因为android:layout_xxx等属性是在根布局中获得的;
第三个参数如果设置为:true,表示xml布局会被解析并加载到“根布局”中,如果为false,则会生成一个view并且不会加载到根布局中,但是这个view的类型取决于第二个参数根布局的类型,所以如果xml布局中如果设置了一些根布局类型中不存在的属性则不会有效果。下边是一个例子:
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_margin="50dp"
android:gravity="center"
android:orientation="horizontal" >
<TextViewandroid:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1" />
<TextViewandroid:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView2" />
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
如果你的adapter中加载布局写成这样即不指定“根布局”:
LayoutInflater.from(context).inflate(R.layout.list_item, null);
运行效果
发现
android:layout_height=”50dp”
android:layout_margin=”50dp”
这两句代码没有效果,这是因为没有指定根布局,所以根布局里设置的android:layout_xxx等属性并没有起作用;
如果将adapter中加载布局代码改为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.list_item, parent, false);
}
return convertView;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
运行效果:
我们发现android:layout_height=”50dp” 已经有效果了
但是layout_margin没有起作用,这是因为我们上边设置的“根布局”是ViewGroup,他不能识别layout_margin属性,在线性布局里才会有这个属性。
如果将adapter中改为:
layout=(LinearLayout)findViewById(R.id.layout1);
View view=LayoutInflater.from(this).inflate(R.layout.list_item,layout , false);
layout.addView(view);
- 1
- 2
- 3
- 1
- 2
- 3
最终运行效果会发现
android:layout_height, android:layout_margin都已经有效果了。
最新文章
- Code Contracts for .NET
- Rectangle Area || LeetCode
- 1606: [Usaco2008 Dec]Hay For Sale 购买干草
- Google Volley: How to send a POST request with Json data?
- [改善Java代码]由点及面,一叶知秋----集合大家族
- WPF页面跳转
- XCode的一些调试技巧
- 做了一个js的拉动遮罩层,两个图片分别显示的效果
- eclipse 快捷键汇总
- POJ 3301 Texas Trip
- FlexGrid简单demo
- File 常用方法
- (转)JAVA堆栈操作
- Java虚拟机一 运行时数据区(栈、堆、方法区等)
- C语言volatile关键字的用法
- matplotlib绘图基本用法-转自(http://blog.csdn.net/mao19931004/article/details/51915016)
- confluence中org.apache.tomcat.util.net.NioEndpoint$Acceptor.run Socket accept failed的解决方法
- Buffer、ArrayBuffer、DataView互转(node.js)
- Leetcode 117
- Java学习笔记29(IO字符流,转换流)
热门文章
- bat如何批量删除指定部分文件夹名的文件夹
- [Winform]setupfactory制作安装包卸载输入密码进行验证
- TrinityCore 魔兽世界私服11159 完整配置
- 报错:System.NotSupportedException: LINQ to Entities does not recognize the method
- 使用HttpClient消费ASP.NET Web API服务
- 解决ASP.NET MVC4中使用Html.DropDownListFor显示枚举值默认项问题
- 初步理解JWT并实践使用
- 【Devops】【docker】【CI/CD】2.docker启动jenkins环境+安装必要的插件
- 暴君第一季/全集Tyrant迅雷下载
- [Web 前端] this作用域问题