关于LayoutInflate.inflate()方法的测试
2015-08-12 14:36
309 查看
首先,解释下inflate的方法之一:
inflate(int resource, ViewGroup root, boolean attachToRoot)
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
结果log提示是LinearLayout
替换
最后,替换
结果log提示是RelativeLayout
就如中文解释的一样,”attached to the root parameter“指的是加载的布局文件的最外层再嵌套一层root布局。
inflate(int resource, ViewGroup root, boolean attachToRoot)
resource | ID for an XML layout resource to load (e.g., R.layout.main_page) |
---|---|
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 (if attachToRoot is false.) |
attachToRoot | Whether 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. |
这个”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布局。
相关文章推荐
- Android Logcat调试中的V、D、I、W、E的分别代表什么?
- Java ClassLoader基础及加载不同依赖 Jar 中的公共类
- js类型检测
- JDBC
- 滚动结果集、参数绑定、连接
- javafx mouseEvent
- DoGet和DoPost
- OMNET问题调试:Error in modeule (cCompoundModule) Tictoc1 (id=1): Class "Txc1" not found -- perhaps its c
- [LeetCode] Populating Next Right Pointers in Each Node
- android QQ第三方登录(一)
- 丢掉鼠标吧,使用最好用的Eclipse快捷键
- Android嵌套滑动组件相关的文章资料
- CentOS最小化安装后启用无线连接网络
- thinkphp 验证码的使用
- [转]linux /proc/cpuinfo 文件分析
- 【札记】设计的五个原则
- 黑马程序员-------------java基础----------------面向对象
- 读张孝详《JAVA就业培训指导》二
- redis主从切换配置
- 单链表的基本操作