在应用中自定义一个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. 003-and design-在create-react-app项目中使用antd

    一.概述 create-react-app 是业界最优秀的 React 应用开发工具之一,本文会尝试在 create-react-app 创建的工程中使用 antd 组件,并自定义 webpack 的 ...

  2. EasyUI Droppable 可放置

    通过 $.fn.droppable.defaults 重写默认的 defaults. 用法 通过标记创建可放置(droppable)区域. <div class="easyui-dro ...

  3. selenium3.0 远程模式

    准备工作: 1. 安装chrome浏览器 2. 下载selnium-server-standalone-3.0.1.jar 步骤: 1. java -jar selnium-server-standa ...

  4. (转)《SSO CAS单点系列》之 实现一个SSO认证服务器是这样的!

    上篇我们引入了SSO这个话题<15分钟了解SSO是个什么鬼!>.本篇我们一步步深入分析SSO实现机理,并亲自动手实现一个线上可用的SSO认证服务器!首先,我们来分析下单Web应用系统登录登 ...

  5. 2.7 The Object Model -- Bindings, Observers, Computed Properties:What do I use when?

    有时候新用户在使用计算属性.绑定和监视者时感到困惑.下面是一些指导方针: 1. 使用computed properties来合成其他属性,以构建新的属性.computed properties不应该包 ...

  6. java之进程和线程

    1.进程和线程的通俗例子. 2.进程和线程关系.

  7. cocos2dx 3.x 拼图小游戏

    .h #define IMAGE_MAX 2 //图片的个数.. //图片结构体 属性 struct IMAGE_DATA { cocos2d::Sprite *m_pImage; bool m_bO ...

  8. 497. Random Point in Non-overlapping Rectangles

    1. 问题 给定一系列不重叠的矩形,在这些矩形中随机采样一个整数点. 2. 思路 (1)一个矩形的可采样点个数就相当于它的面积,可以先依次对每个矩形的面积累加存起来(相当于概率分布中的分布累积函数CD ...

  9. Redis 资料整理

    Redis is an open source, BSD licensed, advanced key-value store. Redis is often referred to as a dat ...

  10. WPF usercontrol 自定义依赖属性

    1.依赖属性不同意一般属性,一般属性主要定义在对象中,而依赖属性是存在一个特殊的依赖属性表中.2.当我们触发改变值时,需要通过SetValue这种方式进行触发. UserControl1.xaml: ...