Android LayoutInflater.inflate使用上的问题解惑
最近在在使用LayoutInflater.inflate方法时遇到了一些问题,以前没有仔细看过此类的使用方法,故将其记录下来,方便日后查阅。
相信大家都知道LayoutInflater.inflate是在android开发遇到的一些使用频率是非常高的方法,如果使用不好的,就会出现一些奇怪的问题。
一个例子如下:
1,一个主布局文件如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"> <ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"> </ListView> </RelativeLayout> 上述的布局文件非常简单,只是在Relative文件中添加了一个ListView. 2,ListView的子View item的布局文件如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:background="@android:color/holo_red_dark"> <TextView
android:id="@+id/list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textSize="20dp"
android:textColor="@android:color/black"/>
</LinearLayout>
生成的预览图如下:为了明显起见,此处将背景设为红色。 3,设置ListView的adapter,adapter的getView方法如下:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = mInflater.inflate(R.layout.list_item,null);
}
TextView textView = (TextView)convertView.findViewById(R.id.list_item);
textView.setText(position + "");
return convertView;
}
最终的样式如下:

可以从测试结果中明显的看出,list_item.xml中设置的高度没有生效,很有可能是ListView的adapter中getView方法中
mInflater.inflate(R.layout.list_item,null),这里的问题。 于是改成 mInflater.inflate(R.layout.list_item,parent,false),再次运行结果如下:上述正是我们想要的结果,下面就针对上述问题结合源码分析下LayoutInflater.inflate方法的使用. 可能有不少的人都不知道LayoutInflater.inflate还有第三个参数,此方法的参数如下:
public View inflate(int resource, ViewGroup root, boolean attachToRoot). 从源码可以得知,不管是 public View inflate(int resource, ViewGroup root)还是
public View inflate(int resource, ViewGroup root, boolean attachToRoot),最终都是调用了
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)。 上述我们例子中调用了
public View inflate(int resource, ViewGroup root),让我们看下此方法的代码如下:
public View inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
}
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
if (DEBUG) System.out.println("INFLATING from resource: " + resource);
XmlResourceParser parser = getContext().getResources().getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
可以发现不管是 public View inflate(int resource, ViewGroup root)还是
public View inflate(int resource, ViewGroup root, boolean attachToRoot),最终都是调用了
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)。 上述例子中调用inflate(R.layout.list_item,null),从上述源码中可以看出最终传给
inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)方法的参数转变为
inflate(XmlPullParser parser,null,false); 下面重点针对
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)方法进行分析,下面只列出了其核心部分的代码
在471行可以看到,当root ! = null时,通过root产生子 view的LayoutParams,此时如果attachToRoot为true,则将此子View 添加进root view,并返回root view; 如果attachToRoot为false则直接返回parser解析的view; 如果root == null,此时直接返回parser解析出来的view,并未设置此view的LayoutParams,此view的LayoutParams将会在被添加时由Parent View赋予值,这就是上述例子第一次的写法没有生效的原因。 针对以上的分析,归纳如下:
调用
LayoutInflater.inflate方法,并且将root参数设置为null,就等于忽略了xml布局文件中的layout_×参数如果
root不为null,并且attachRoot=true,那么就会根据root生成一个布局文件View的LayoutParam对象,并且将这个View添加到root中去,并且返回这个root的View因此,最好还是使用这个代码吧:
View v1 = LayoutInflater.from(this).inflate(R.layout.layout_menu_item, layout, false);
Android LayoutInflater.inflate使用上的问题解惑的更多相关文章
- android LayoutInflater.inflate()的参数介绍
LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...
- Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解
方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图 ...
- Android LayoutInflater.inflate()的参数及其用法
很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...
- Android LayoutInflater.inflate源码解析
一年多以前看过源码,感觉了解比较透彻了,长时间不经大脑思考,靠曾经总结的经验使用inflate方法,突然发现不知道什么时候忘记其中的原理了,上网查了一些资料,还各有不同,反而把我搞糊涂了,还是自己看源 ...
- 6、Android之LayoutInflater.inflate()
LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...
- android LayoutInflater和inflate()方法的用法(转载)
原文出处:http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html 在实际开发中LayoutInflater这个类还是非常有用的,它的作用 ...
- Android LayoutInflater详解
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且 ...
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 有不少朋友跟我反应,都希望我可以写一篇关于View的文章,讲一讲Vi ...
- Android之Inflate()
Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能.最近做的一个项目就是这一点让我迷茫了好几天. Android上还有一个与Inflate( ...
随机推荐
- 新发现:原来java正则表达式不写^和$也可以运行
最近用了好多正则表达式,都是循规蹈矩的在前面加上^在后面加上$ 像这个样子"^[.]\\S+$",但实际上我在eclipse和editplus下都试了一下,不加前缀和后缀也是可以的 ...
- 团队项目·冰球模拟器——cmake 自动化构建系统的配置文件的编写
1 前言 考虑到命令行界面下编译程序并不如在 IDE 那么直观,再考虑到各位队友对 Linux 并不熟悉,如何大幅度地减轻整个项目的开发复杂度就是一个很重要的问题. 在 Linux 下有个很古老但很有 ...
- JS/JQ综合总结
总结 js部分 一 语法结构 1 区分大小写 2注意 //单行 /*多行注释*/ 3子面量(直接量 literal) 12//数字 5.8//小数 "hello"字符串 true ...
- index of rmvb mp3 rm突破站点入口下载
首先打开Google,在关键词输入框中输入"index of/"inurl:lib(双引號为英文状态下) ,选择“搜索中文简体网页”选项,回车搜索,得到了一些网页,不要以为这是一些 ...
- Android View.onMeasure方法的理解(转载)
一下内容转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure(计算)和layout(布局).1 ...
- C、Shell、Perl基于Tomcat开发CGI程序环境配置
基于Tomcat7.0版本号配置CGI开发环境,步聚例如以下: 以我的Tomcat7安装文件夹为例:TOMCA_HOME = /Users/yangxin/Documents/devToos/java ...
- 在hadoop上进行编写mapreduce程序,统计关键词在text出现次数
mapreduce的处理过程分为2个阶段,map阶段,和reduce阶段.在要求统计指定文件里的全部单词的出现次数时. map阶段把每一个关键词写到一行上以逗号进行分隔.并初始化数量为1(同样的单词h ...
- MVC - HtmlHelper类
传统的Html元素不能和服务端数据进行绑定 HtmlHelper类提供了一系列的方法来生成Html元素 并可以实现与数据绑定在一起 然后生成Html Html.BeginForm(actionName ...
- HD1285(拓扑排序)
package cn.hncu.dataStruct.search.topSort; import java.util.Scanner; public class Hdu1285 { static S ...
- HINSTANCE数据类型
作者:马 岩(Furzoom) (http://www.cnblogs.com/furzoom/)版权声明:本文的版权归作者与博客园共同所有.转载时请在明显地方注明本文的详细链接,未经作者同意请不要删 ...
为了明显起见,此处将背景设为红色。
3,设置ListView的adapter,adapter的getView方法如下:
上述正是我们想要的结果,下面就针对上述问题结合源码分析下LayoutInflater.inflate方法的使用.
可能有不少的人都不知道LayoutInflater.inflate还有第三个参数,此方法的参数如下: