android中LayoutInflater.from(context).inflate的分析

原文地址为: android中LayoutInflater.from(context).inflate的分析

在应用中自定义一个view,需要获取这个view的布局,需要用到

(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);

这个方法。

一般的资料中的第二个参数会是一个null。通常情况下没有问题,但是如果我想给这个view设置一个对应的类,然后通过这个类来操作的话就会出问题。

先看下面的例子

 1 <?xml version="1.0" encoding="utf-8"?>
2
3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4 android:orientation="vertical"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:background="@color/white">
8
9 <TextView
10 android:layout_width="match_parent"
11 android:layout_height="wrap_content"
12 android:id="@+id/textViewTitle"
13 android:textColor="@color/black"
14 android:gravity="center" android:textSize="26dp"/>
15
16 <TextView
17 android:layout_width="match_parent"
18 android:layout_height="wrap_content"
19 android:id="@+id/textViewAuthor"
20 android:layout_gravity="left" android:textColor="@android:color/darker_gray" android:textSize="16dp"/>
21
22 <ImageView
23 android:layout_width="wrap_content"
24 android:layout_height="wrap_content"
25 android:id="@+id/imageView"
26 android:layout_gravity="center_horizontal"
27 android:scaleType="center"/>
28
29 <TextView
30 android:layout_width="match_parent"
31 android:layout_height="wrap_content"
32 android:id="@+id/textViewContent"
33 android:layout_gravity="center_horizontal" android:textColor="@color/black" android:textSize="20dp"/>
34
35 <LinearLayout
36 android:layout_width="fill_parent"
37 android:layout_height="2dp"
38 android:layout_gravity="center"
39 android:background="@color/black">
40 </LinearLayout>
41
42 <TextView
43 android:layout_width="match_parent"
44 android:layout_height="wrap_content"
45 android:id="@+id/textViewOtherInfo"
46 android:layout_gravity="left" android:clickable="true" android:textColor="@android:color/darker_gray"
47 android:textSize="16dp"/>
48 </LinearLayout>

对应的类是

 1 public class ContentItemView extends LinearLayout {
2
3 private TextView title;
4 private TextView author;
5 private TextView content;
6 private TextView otherInfo;
7 private ImageView contentImage;
8
9 private ContentInfo info;
10
11 public ContentItemView(Context context) {
12 super(context);
13 init(context);
14 }
15
16 private void init(Context context) {
17 LinearLayout convertView =
18 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);
19 title = (TextView) convertView.findViewById(R.id.textViewTitle);
20 author = (TextView) convertView.findViewById(R.id.textViewAuthor);
21 content = (TextView) convertView.findViewById(R.id.textViewContent);
22 otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo);
23 contentImage = (ImageView) convertView.findViewById(R.id.imageView);
24 }
25 }

 

这个自定义view我想将它添加到一个listview中。

 1     public void add(final ContentInfo info) {
2 ContentItemView contentItemView = new ContentItemView(context);
3 contentItemView.setContentInfo(info);
4 contentItemView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
5
6 data.add(contentItemView);
7 }
8
9 @Override
10 public View getView(int position, View convertView, ViewGroup parent) {
11 return data.get(position);
12 }

程序运行起来以后,没有任何问题,但是界面没有显示出来,仅仅是在listview中多了一系列黑色的条条

 

如果将

(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);

修改为

(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, this);

显示就会正常

 

上面的东西很多资料里面都有,但是原因是什么?我在网络上找了很久都没有找到,于是就自己研究了下代码

 1     public View inflate(int resource, ViewGroup root) {
2 return inflate(resource, root, root != null);
3 }
4
5 public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
6 if (DEBUG) System.out.println("INFLATING from resource: " + resource);
7 XmlResourceParser parser = getContext().getResources().getLayout(resource);
8 try {
9 return inflate(parser, root, attachToRoot);
10 } finally {
11 parser.close();
12 }
13 }
14 public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
15 ........
16 if (TAG_MERGE.equals(name)) {
17 if (root == null || !attachToRoot) {
18 throw new InflateException("<merge /> can be used only with a valid "
19 + "ViewGroup root and attachToRoot=true");
20 }
21
22 rInflate(parser, root, attrs, false);
23 } else {
24 // Temp is the root view that was found in the xml
25 View temp;
26 if (TAG_1995.equals(name)) {
27 temp = new BlinkLayout(mContext, attrs);
28 } else {
29 temp = createViewFromTag(root, name, attrs);
30 }
31
32 ViewGroup.LayoutParams params = null;
33
34 if (root != null) {
35 if (DEBUG) {
36 System.out.println("Creating params from root: " +
37 root);
38 }
39 // Create layout params that match root, if supplied
40 params = root.generateLayoutParams(attrs);
41 if (!attachToRoot) {
42 // Set the layout params for temp if we are not
43 // attaching. (If we are, we use addView, below)
44 temp.setLayoutParams(params);
45 }
46 }
47
48 ..............
49 if (root != null && attachToRoot) {
50 root.addView(temp, params);
51 }
52
53 // Decide whether to return the root that was passed in or the
54 // top view found in xml.
55 if (root == null || !attachToRoot) {
56 result = temp;
57 }
58 }
59 .....
60 }

可以看到在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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 这个方法是在Android开发使用的,可以通过LayoutInflater类的from方法获取到一个LayoutInflater实例,然后调用其inflate方法来加载布局文件,将其转化为一个View对象,以供使用。 ### 回答2: layoutinflater.from(context).inflateAndroid一个比较常见的方法,主要用于将布局文件转换成可在代码使用的View对象。以下是详细的解释: 首先,LayoutInflater是一个Android系统类,其作用是将布局文件转换成可在代码实现的View对象。而从context.getParameter()方法返回一个LayoutInflater示例后,需要使用其inflate()方法来加载布局文件,返回一个View对象。inflate()方法有三个参数:布局文件ID、父View以及一个布尔标志。 在大多数情况下,第二个参数都为null,这意味着在加载布局文件时没有父元素。而第三个参数标志通常设置为false,这意味着在加载布局文件时不附加给指定父元素。 因此,调用LayoutInflater.from(context).inflate(R.layout.my_layout, null, false)会返回一个View对象,该对象表示my_layout.xml布局文件的内容。可以将此对象添加到任何视图层次结构,例如: ViewGroup parent = findViewById(R.id.parent_layout); View child = LayoutInflater.from(context).inflate(R.layout.my_layout, parent, true); parent.addView(child); 在这种情况下,inflate()方法的第二个参数是父元素的引用,表示新加载的View对象将成为此父元素的一部分。第三个参数标志设置为true,这意味着从布局文件加载的视图将自动成为传递给inflate()方法的父元素的一部分。 总之,LayoutInflater.from(context).inflateAndroid开发非常有用的一个方法,它使您可以轻松地将布局文件转换为可在代码操作的View对象。了解它的用法可以使您更轻松地开发高质量的Android应用程序。 ### 回答3: layoutinflater.from(context).inflateAndroid 一种常见的布局填充方法。在 Android ,我们通常使用 XML 文件创建布局,然后使用 Java 代码调用该布局以填充视图。其layoutinflater.from(context) 是获取一个 LayoutInflater 对象的方法,它可以用于动态将布局文件转换为其对应的视图对象并在当前视图添加。 在上述代码context 是用于创建视图的上下文对象,inflate 是用于执行布局填充的方法。该方法需要传入一个布局文件ID,该 ID 用于确定要填充的布局文件的位置和名称。被填充的布局文件包含了布局的所有 View 对象及其属性,包括控件大小、边距、背景等等。填充完成后,该布局文件的所有视图都将被转换为 Java 的 View 类型,并作为整个填充视图的一部分添加到 ViewGroup 。 通常情况下,我们会在 Activity 调用该方法以填充视图对象,然后通过 findViewById 对填充好的视图对象的控件进行定位,并对其进行任何必要的更改或操作。这样,我们就可以通过代码实现 UI 界面各种复杂的布局效果,并实现超出 XML 文件所能实现的更高级的 UI 界面效果。 总之,layoutinflater.from(context).inflateAndroid 一种非常常见的布局填充方法,它允许我们将布局文件转换为对应的 View 对象,并添加到当前视图。这种方法在 Android 应用程序开发非常有用,特别是在创建 UI 界面时。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值