Android LayoutInflater.from(context).inflate
在应用中自定义一个view,需要获取这个view的布局,需要用到
(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);
这个方法。
一般的资料中的第二个参数会是一个null。通常情况下没有问题,但是如果我想给这个view设置一个对应的类,然后通过这个类来操作的话就会出问题。
先看下面的例子
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewTitle"
android:textColor="@color/black"
android:gravity="center" android:textSize="26dp"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewAuthor"
android:layout_gravity="left" android:textColor="@android:color/darker_gray" android:textSize="16dp"/> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal"
android:scaleType="center"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewContent"
android:layout_gravity="center_horizontal" android:textColor="@color/black" android:textSize="20dp"/> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_gravity="center"
android:background="@color/black">
</LinearLayout> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewOtherInfo"
android:layout_gravity="left" android:clickable="true" android:textColor="@android:color/darker_gray"
android:textSize="16dp"/>
</LinearLayout>
对应的类是
public class ContentItemView extends LinearLayout {
private TextView title;
private TextView author;
private TextView content;
private TextView otherInfo;
private ImageView contentImage;
private ContentInfo info;
public ContentItemView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
LinearLayout convertView =
(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);
title = (TextView) convertView.findViewById(R.id.textViewTitle);
author = (TextView) convertView.findViewById(R.id.textViewAuthor);
content = (TextView) convertView.findViewById(R.id.textViewContent);
otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo);
contentImage = (ImageView) convertView.findViewById(R.id.imageView);
}
}
这个自定义view我想将它添加到一个listview中。
public void add(final ContentInfo info) {
ContentItemView contentItemView = new ContentItemView(context);
contentItemView.setContentInfo(info);
contentItemView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
data.add(contentItemView);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return data.get(position);
}
程序运行起来以后,没有任何问题,但是界面没有显示出来,仅仅是在listview中多了一系列黑色的条条

如果将
(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);
修改为
(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, this);
显示就会正常

上面的东西很多资料里面都有,但是原因是什么?我在网络上找了很久都没有找到,于是就自己研究了下代码
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(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
........
if (TAG_MERGE.equals(name)) {
if (root == null || !attachToRoot) {
throw new InflateException("<merge /> can be used only with a valid "
+ "ViewGroup root and attachToRoot=true");
}
rInflate(parser, root, attrs, false);
} else {
// Temp is the root view that was found in the xml
View temp;
if (TAG_1995.equals(name)) {
temp = new BlinkLayout(mContext, attrs);
} else {
temp = createViewFromTag(root, name, attrs);
}
ViewGroup.LayoutParams params = null;
if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
..............
if (root != null && attachToRoot) {
root.addView(temp, params);
}
// Decide whether to return the root that was passed in or the
// top view found in xml.
if (root == null || !attachToRoot) {
result = temp;
}
}
.....
}
可以看到在inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)函数中,只有root不等于空的情况下才能够真正的把view添加到listview中。
看看参数root的含义:@param root Optional view to be the parent of the generated hierarchy
就是说这个表示的事view的容器是什么。如果不告诉SDK你要把这个view放到哪里,当然就不能生成view了。
Android LayoutInflater.from(context).inflate的更多相关文章
- android中LayoutInflater.from(context).inflate的分析
在应用中自定义一个view,需要获取这个view的布局,需要用到 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.conten ...
- 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.inflate使用上的问题解惑
最近在在使用LayoutInflater.inflate方法时遇到了一些问题,以前没有仔细看过此类的使用方法,故将其记录下来,方便日后查阅. 相信大家都知道LayoutInflater.inflate ...
- Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解
方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图 ...
- Android LayoutInflater详解
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且 ...
- 转载《Android LayoutInflater详解》
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 有不少朋友跟我反应,都希望我可以写一篇关于View的文章,讲一讲Vi ...
- Android LayoutInflater详解(转)
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
随机推荐
- SQL Server 2012 BI 学习 第一天
了解数据源,数据源视图,多维数据集,维度 数据源:一个数据库或者其它数据链接,SSAS不支持使用模拟功能来处理 OLAP 对象.模拟信息选择“使用服务帐户” 数据源视图:DSV是元数据的单个统一视图, ...
- IOS开发一些资源收集
从别的地方看到的,多谢作者,现贴在这里备忘. 在线教程 iOS技术概述 我个人感觉这是开始iOS开发第一步,了解一下iOS整体的结构,有哪些支持类库.如何使用类库等等一切基础的概念性指示 iPh ...
- Entity Framework学习笔记(五)----Linq查询(2)---贪婪加载
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们使用了Linq对Entity Framework进行了一个查询,但是通过学习我们却发现了懒加载给我来的性能上的 ...
- java package and import
1.Package Package类的主要作用是解决命名冲突.package中所存放的所有文件,一般分一下就分这三种 1,java程序源文件,扩展名为.java. 2,编译好的java类文件,扩展名为 ...
- ApplicationContext
参考网址: http://baike.baidu.com/link?url=IPzNiVScxSd6ijhDeCKKEuywPqisDeTfyYSQIPRZqLxy6onkPddfzyvcWQC6_M ...
- 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数。
// test20.cpp : 定义控制台应用程序的入口点. // include "stdafx.h" include include include include inclu ...
- 【转载】关于typedef的用法总结
不管实在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中.typedef与#define有些相似,但更多的是不同,特别是在一些复杂的用法上,就完全不同了,看了网上一些 ...
- C++中的mutable关键字
mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词. 在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量,将永远 ...
- ARRAY_SIZE宏
宏ARRAY_SIZE,是求设备结构体中设备的个数, 定义在linux/kernel.h中 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof((arr)[ ...
- hadoop+hbase
hadoop的配置见下面这篇文章 http://www.powerxing.com/install-hadoop-2-4-1-single-node/ Hadoop安装教程_单机/伪分布式配置_Had ...