在应用中自定义一个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的分析的更多相关文章

  1. Android LayoutInflater.from(context).inflate

    在应用中自定义一个view,需要获取这个view的布局,需要用到 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.conten ...

  2. Android中相机和相冊使用分析

    Android中相机和相冊使用分析 欢迎转载,但请尊重原创(文章来自不易,转载请标明转载出处,谢谢) 在手机应用程序中,使用自带的相机拍照以及相冊选择喜欢的图片是最常见只是的用户需求,那么怎么合理使用 ...

  3. Android 中View的绘制机制源代码分析 一

    尊重原创: http://blog.csdn.net/yuanzeyao/article/details/46765113 差点儿相同半年没有写博客了,一是由于工作比較忙,二是认为没有什么内容值得写, ...

  4. Android 中View的绘制机制源代码分析 三

    到眼下为止,measure过程已经解说完了,今天開始我们就来学习layout过程.只是在学习layout过程之前.大家有没有发现我换了编辑器,哈哈.最终下定决心从Html编辑器切换为markdown编 ...

  5. Android 中View的绘制机制源代码分析 二

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/46842891 本篇文章接着上篇文章的内容来继续讨论View的绘制机制,上篇文章中我们主要解说 ...

  6. Android 中 LayoutInflater 的使用

    一.LayoutInflater 的作用 我们一般使用 LayoutInflater 做一件事:View inflate(int resource, ViewGroup root); inflate( ...

  7. Android中LayoutInflater的使用

    Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater 的作用类似于 findViewById(),不同点是LayoutInflater是用来找layou ...

  8. android中LayoutInflater详解与使用

    android的LayoutInflater用来得到一个布局文件,也就是xxx.xml,而我们常用的findviewbyid是用来取得布局文件里的控件或都布局.inflater即为填充的意思,也就是说 ...

  9. 深入理解 Android 中的各种 Context

    前言 网上关于 Context 的文章也已经有不少了,比如值得参考的有: Android Context完全解析,你所不知道的Context的各种细节 Android Context 到底是什么? 但 ...

随机推荐

  1. 商铺项目(Logback配置与使用)

    <?xml version="1.0" encoding="utf-8"?> <configuration debug="false ...

  2. 【云安全与同态加密_调研分析(8)】同态加密技术及其应用分析——By Me

    ◆同态加密技术(Homomorphic Encryption, HE)及其应用◆ ◆加密方案◆ ◆应用领域◆ ◆厂商◆ ◆同态加密现有产品形态和工程实现◆ ◆参考链接◆ ◆备注(其他参考信息)◆ 同态 ...

  3. 《Java程序猿面试笔试宝典》之Java程序初始化的顺序是如何的

    在Java语言中.当实例化对象时.对象所在类的全部成员变量首先要进行初始化,仅仅有当全部类成员完毕初始化后,才会调用对象所在类的构造函数创建对象.    Java程序的初始化一般遵循以下三个原则(以下 ...

  4. Elasticsearch入门教程

    ElasticSearch是一个高度可扩展的开源搜索引擎并使用REST API,所以您值得拥有. 在本教程中,将介绍开始使用ElasticSearch的一些主要概念. 下载并运行ElasticSear ...

  5. gb28181的SPVMN测试环境搭建以及设备端和服务器的具体实现

    1.GB/T28181开发1之SPVMN(1.0.0.1)环境搭建 https://blog.csdn.net/hiwubihe/article/details/82910685 2.SPVMN 视频 ...

  6. soapUI-DataSource

    1.1.1.1 概述 - 数据源   Option Description   Properties DataSource属性表   Toolbar DataSource工具栏   Configura ...

  7. SpringData环境搭建代码编写

    首先我们在前面的两节已经了解了SpringData是干什么用的?那我们从这节开始我们就开始编码测试SpringData. 1:首先我们从配置文件开始,我们首先需要写一个连接数据库的文件db.prope ...

  8. net.tcp协议的wcf服务在远程计算机无法调用问题分析

    可能原因1:net.tcp监听端口服务没有启动. 可能原因2:防火墙阻止了端口服务器路径访问. 可能原因3:配置文件路径endpoint路径和引用路径不一致 可能原因4:权限受限制.

  9. devise修改密码

    https://ruby-china.org/topics/1314 password/edit不是给你直接改密码用的 这个是忘记密码后,发送重置密码的邮件到你邮箱,同时生成一个token 然后你点那 ...

  10. Ubuntu 添加用户到 sudoer

    一.概述 新建用户后,我们可能需要该用户能够使用一些越权的东西.sudo命令能够暂时提升该用户的权限到root,但是前提是要求该用户存在与 sudoer list 中. sudoers 存储在 /et ...