关于LayoutInflater.from(context).inflate()的使用的问题

在一般项目中使用adapter时,加载item布局咱们一般会使用:
[html]  view plain copy
  1.     LayoutInflater.from(context).inflate(R.layout.list_item, null);  

      但这样你会发现编译器不希望你这样:Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)

       而且你的xml的最外层布局的一些对于其父布局的一些诉求属性,不管怎么设置都不起作用。

比如这样一个item布局:

[html]  view plain copy
  1. <</span>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="50dp"  
  4.     android:layout_margin="50dp"  
  5.     android:gravity="center"  
  6.     android:orientation="horizontal" >  
  7.   
  8.     <</span>TextView  
  9.         android:id="@+id/textView1"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="TextView1" />  
  13.   
  14.     <</span>TextView  
  15.         android:id="@+id/textView2"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="TextView2" />  
  19.   
  20. </</span>LinearLayout>  

     用上述方法加载后结果发现


[html]  view plain copy
  1. android:layout_height="50dp"  
  2.   android:layout_margin="50dp"  
      这两句没有效果。

   so,咱们来研究一下。

  网上流传了这样一篇文章,

http://www.doubleencore.com/2013/05/layout-inflation-as-intended/
点击打开链接

这是一个老外对inflate()的研究。

  其实他有很多个方法,但查看源码这些方法都殊途同归。

 我现在就说一下inflate(int resource, ViewGroup root, boolean attachToRoot)

第一个参数无需过多解释。

第二个参数指的是加载布局的root

Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (ifattachToRoot is false.)

大概就是说如果后面attachToRoot为true的情况下,这个布局会被解析并加载在root下面,如果为false,则会依照root去解析该xml并返回view,但是这个view不会被加载到root里。

其实如果为false,就是讲xml解析了,并依照root的类型给生成的view set一个LayoutParams ,但不将其add到root里。

然后咱们看源代码里

[html]  view plain copy
  1. LayoutInflater.from(context).inflate(R.layout.list_item, null);  

 这个其实 是这样调用的: 

[java]  view plain copy
  1. public View inflate(int resource, ViewGroup root)  
  2.     return inflate(resource, root, root != null);  
  3.  
所以我建议将其写为
[java]  view plain copy
  1. LayoutInflater.from(context).inflate(R.layout.list_item,root,false);  
root就是加载这个view的父布局。

然后再在listview的adapter试一下,

[java]  view plain copy
  1. @Override  
  2. public View getView(int position, View convertView, ViewGroup parent)  
  3.     if (convertView == null 
  4.         convertView LayoutInflater.from(context).inflate(  
  5.                 R.layout.list_item, parent, false);  
  6.         
  7.   
  8.     return convertView;  
  9.  


发现在listview里加载item的布局,


[html]  view plain copy
  1. android:layout_height="50dp"  
这句已经起到作用,但layout_margin无效果。

这是因为在listview里,convertview用的是viewgroup的 LayoutParams,所以线性布局的一些属性,例如layout_margin在解析的时候不起作用的。

为了验证一下,咱们在linearlayout中实验一下LayoutInflater

[java]  view plain copy
  1. layout=(LinearLayout)findViewById(R.id.layout1);      
  2. View view=LayoutInflater.from(this).inflate(R.layout.list_item,layout false);  
  3.         layout.addView(view);  


这样的话设置宽高和设置layout_margin都起到了作用。因为这时候view的layoutParams是LinearLayout.layoutParams的缘故,所以layout_margin果断会起到效果。

奥,对了,注意,在listview中不要将inflate(int resource, ViewGroup root, boolean attachToRoot)的attachToRoot设为true,

因为这样等于说让listview addview(convertView),但是listview不能加子控件,会报如下错误:

[html]  view plain copy
  1. java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView  

而在linearlayout里可以将inflate(int resource, ViewGroup root, boolean attachToRoot)的attachToRoot设为true,这样就相当于
[java]  view plain copy
  1. View view=LayoutInflater.from(this).inflate(R.layout.list_item,layout false);  
  2.         layout.addView(view);  

最后附上demo下载地址http://download.youkuaiyun.com/detail/ccfcccfc/8142913点击打开链接




转自:http://blog.sina.com.cn/s/blog_7d95a2e70102v9xr.html,thanks!

### 回答1: 这个方法是在Android开发中使用的,可以通过LayoutInflater类的from方法获取到一个LayoutInflater实例,然后调用其inflate方法来加载布局文件,将其转化为一个View对象,以供使用。 ### 回答2: layoutinflater.from(context).inflate是Android中一个比较常见的方法,主要用于将布局文件转换成可在代码中使用的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).inflate是Android开发中非常有用的一个方法,它使您可以轻松地将布局文件转换为可在代码中操作的View对象。了解它的用法可以使您更轻松地开发高质量的Android应用程序。 ### 回答3: layoutinflater.from(context).inflate 是 Android 中一种常见的布局填充方法。在 Android 中,我们通常使用 XML 文件创建布局,然后使用 Java 代码调用该布局以填充视图。其中,layoutinflater.from(context) 是获取一个 LayoutInflater 对象的方法,它可以用于动态将布局文件转换为其对应的视图对象并在当前视图中添加。 在上述代码中,context 是用于创建视图的上下文对象,inflate 是用于执行布局填充的方法。该方法中需要传入一个布局文件ID,该 ID 用于确定要填充的布局文件的位置和名称。被填充的布局文件中包含了布局中的所有 View 对象及其属性,包括控件大小、边距、背景等等。填充完成后,该布局文件中的所有视图都将被转换为 Java 中的 View 类型,并作为整个填充视图的一部分添加到 ViewGroup 中。 通常情况下,我们会在 Activity 中调用该方法以填充视图对象,然后通过 findViewById 对填充好的视图对象中的控件进行定位,并对其进行任何必要的更改或操作。这样,我们就可以通过代码实现 UI 界面中各种复杂的布局效果,并实现超出 XML 文件所能实现的更高级的 UI 界面效果。 总之,layoutinflater.from(context).inflate 是 Android 中一种非常常见的布局填充方法,它允许我们将布局文件转换为对应的 View 对象,并添加到当前视图中。这种方法在 Android 应用程序开发中非常有用,特别是在创建 UI 界面时。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值