关于inflate的参数 简单分享

方法 inflate(int resource, ViewGroup root, boolean attachToRoot) 中,前两个参数都好理解,资源文件和父视图java

第3个参数表示是否添加到父视图中android

举个例子看一下布局

新建一个工程this

 

工程包含两个xml文件spa

layout/main.xmlcode

<?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" />
   
    <LinearLayout
        android:id="@+id/ffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        />
 
</LinearLayout>

layout/ffff.xmlorm

<?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" >
    
    <Button 
        android:layout_width="89dp"
        android:layout_height="89dp"
        />

</LinearLayout>


setContentView(R.layout.main);xml

ViewGroup v = (ViewGroupfindViewById(R.id.ffff);utf-8

View vv = LayoutInflater.from(this).inflate(R.layout.ffffv);资源

上面红色表示意思

LinearLayout root = (LinearLayout) findViewById(R.id.ffff);

LayoutInflater layoutInflater = LayoutInflater.from(this);

layoutInflater.inflate(R.layout.ll, root,true);

这样布局里就会多个button

若是layoutInflater.inflate(R.layout.ll, root,false);

则布局里没有button

相关文章
相关标签/搜索