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

在Android开发中,使用LayoutInflater.from(context).inflate()加载Adapter的item布局时,可能会遇到编译警告和布局参数问题。文章探讨了inflate()方法的参数含义,特别是root和attachToRoot的作用。通过源码分析和实例演示,解释了当attachToRoot为false时,布局不会添加到root中,而在ListView中不应将attachToRoot设为true,以避免添加子视图的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      在一般项目中使用adapter时,加载item布局咱们一般会使用:

    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布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_margin="50dp"
    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView2" />

</LinearLayout>

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


  android:layout_height="50dp"
    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里。

然后咱们看源代码里

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

 这个其实 是这样调用的: 

    public View inflate(int resource, ViewGroup root) {
        return inflate(resource, root, root != null);
    }
所以我建议将其写为

LayoutInflater.from(context).inflate(R.layout.list_item,root,false);
root就是加载这个view的父布局。

然后再在listview的adapter试一下,

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.list_item, parent, false);
		}	

		return convertView;
	}


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


android:layout_height="50dp"
这句已经起到作用,但layout_margin无效果。

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

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

layout=(LinearLayout)findViewById(R.id.layout1);	
View view=LayoutInflater.from(this).inflate(R.layout.list_item,layout , false);
		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不能加子控件,会报如下错误:

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

而在linearlayout里可以将inflate(int resource, ViewGroup root, boolean attachToRoot)的attachToRoot设为true,这样就相当于

View view=LayoutInflater.from(this).inflate(R.layout.list_item,layout , false);
		layout.addView(view);

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






评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值