inflate中的参数

来源:互联网 发布:javascript 书 编辑:程序博客网 时间:2024/05/17 23:35

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

resource : 需要加载的Layout的xml资源
root          :      表示获得容器视图对象后,要将获得的该对象添加到哪个容器视图对象中,  如果不想将要获得的对象添加到任何其他容器中 则为NULL
attachToRoot  :   是否和root关联


注意:     如果root被提供而且attachToRoot为TRUE的话,在把新生成的View连接到root,返回root.否者返回的是新生成的View.
              如果root被提供但attachToRoot为FALSE的话,root只是把它的LayoutParams参数给新生成的View用,不会把新生成的View连接到root  .  当然返回的是新生成的View

对于第三个参数请看如下实验:

main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >     <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <FrameLayout        android:id="@+id/ffff"        android:layout_width="match_parent"        android:layout_height="wrap_content"></FrameLayout> </LinearLayout>

ffff.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >     <CheckBox        android:id="@+id/checkBox1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="CheckBox" /> </LinearLayout>
activity 中的部分代码:

setContentView(R.layout.main);ViewGroup v = (ViewGroup) findViewById(R.id.ffff);View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v);
效果如下:


视图层次结构如下:


如activity代码为:

View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v, false);
效果如下:


层次结构如下:


如果activity中的代码如下:

ViewGroup v = (ViewGroup) findViewById(R.id.ffff);View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v, false);v.addView(vv);

效果如下:












0 0