方法 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 = (ViewGroup) findViewById(R.id.ffff);utf-8
View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v);資源
上面紅色表示意思
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