关于LayoutInflate.inflate()方法的测试


首先,解释下inflate的方法之一:

inflate(int resource, ViewGroup root, boolean attachToRoot) 


resourceID for an XML layout resource to load (e.g., R.layout.main_page)
rootOptional 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 (if attachToRoot is false.)
attachToRootWhether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
Returns:The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.
这个” attached to the root parameter“不明白是什么意思,查了个中文的解释:

1. 如果root为null,attachToRoot将失去作用,设置任何值都没有意义。

2. 如果root不为null,attachToRoot设为true,则会在加载的布局文件的最外层再嵌套一层root布局。

3. 如果root不为null,attachToRoot设为false,则root参数失去作用。

4. 在不设置attachToRoot参数的情况下,如果root不为null,attachToRoot参数默认为true。

来源: http://blog.163.com/cumt_xl/blog/static/190715044201471543543790/


测试文件:

activity_main.xml,是一个空的RelativityLayout

<!-- activity_main -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</RelativeLayout>

<!-- text_view -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />


package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		LinearLayout llt = new LinearLayout(this);
		RelativeLayout rlt = (RelativeLayout)findViewById(R.id.main_layout);
		
		LayoutInflater inflater  = LayoutInflater.from(this);
		View v = inflater.inflate(R.layout.text_view, llt, true);//这行是关键
		rlt.addView(v);
		
		ViewParent vp = findViewById(R.id.text_view).getParent();
		Log.d("TAG", "the parent of mainLayout is " + vp);  
	}
}

结果log提示是LinearLayout



替换

View v = inflater.inflate(R.layout.text_view, llt, true);

View v = inflater.inflate(R.layout.text_view, llt, false);
结果log提示是RelativeLayout



最后,替换

View v = inflater.inflate(R.layout.text_view, llt, true);

View v = inflater.inflate(R.layout.text_view, null);

结果log提示是RelativeLayout


就如中文解释的一样,”attached to the root parameter“指的是加载的布局文件的最外层再嵌套一层root布局。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值