您的位置:首页 > 其它

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

2015-08-12 14:36 309 查看
首先,解释下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布局。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: