LayoutInflater的inflate方法,在fragment的onCreateView方法中經常用到:
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
LayoutInflater的inflate方法一共有四種,但我們日常用經常用到的就只有這兩種:
- public View inflate(int resource, ViewGroup root) {
- return inflate(resource, root, root != null);
- }
- public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
- if (DEBUG) System.out.println("INFLATING from resource: " + resource);
- XmlResourceParser parser = getContext().getResources().getLayout(resource);
- try {
- return inflate(parser, root, attachToRoot);
- } finally {
- parser.close();
- }
- }
所以,這裏直接介紹裏面調用這個方法即可:
- public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
在這個方法裏面,上半部分爲xml解析的代碼,這裏就不貼出來,確實沒什麼東西可看。直接看中間部分的代碼:
- ViewGroup.LayoutParams params = null;
- if (root != null) {
- if (DEBUG) {
- System.out.println("Creating params from root: " +
- root);
- }
- // Create layout params that match root, if supplied
- params = root.generateLayoutParams(attrs);
- if (!attachToRoot) {
- // Set the layout params for temp if we are not
- // attaching. (If we are, we use addView, below)
- temp.setLayoutParams(params);
- }
- }
params = root.generateLayoutParams(attrs);
這段的意思是:如果調用inflate方法,傳入了ViewGroup root參數,則會從root中得到由layout_width和layout_height組成的LayoutParams,在attachToRoot設置爲false的話,就會對我們加載的視圖View設置該LayoutParams。
接着往下看:
- // We are supposed to attach all the views we found (int temp)
- // to root. Do that now.
- if (root != null && attachToRoot) {
- root.addView(temp, params);
- }
- // Decide whether to return the root that was passed in or the
- // top view found in xml.
- if (root == null || !attachToRoot) {
- result = temp;
- }
root.addView(temp, params);
如果設置了ViewGroup root參數,且attachToRoot設置爲true的話,則將我們加載的視圖做爲子視圖添加到root視圖中。
如果我們ViewGroup root設置爲空的話,就直接返回我們創建的視圖;如果root不爲空,且attachToRoot設置爲false的話,就返回上面那段:對我們加載的視圖View設置該LayoutParams。
接上篇,接下來,就用最最簡單的例子來說明一下:
用兩個佈局文件main 和 test:
其中,main.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" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:gravity="center"
- android:text="hello world" />
- </LinearLayout>
test.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="200dp"
- android:background="#ffffff00"
- android:orientation="vertical" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:gravity="center"
- android:text="test" />
- </LinearLayout>
在test中設置了其高度爲200dp,並且設置了背景顏色。
接下來看一下LayoutInflater().inflate方法實現:
第一種方式:inflate(view, null)
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
- null);
- view = getLayoutInflater().inflate(R.layout.test, null);
- setContentView(view);
- }
運行的效果如下:
這個就很容易理解了,因爲我沒有指定ViewGroup root參數,所以,相當於直接加載了test視圖文件,並返回。
而它的高度充滿了全屏而不是200dp,因爲執行inflate的時候,沒有root參數,則無法爲test視圖設定layoutparam參數。那麼爲什麼會充滿屏幕而不是沒有顯示呢?是因爲我們將其設置視圖到activity時,會取得當前window的layoutparam賦值給它,也就是充滿全屏。有興趣的話,你可以改一下test的layout_width設定一個數值,最後運行效果是一樣的。
第二種方式:inflate(view, root, false)
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
- null);
- view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view, false);
- setContentView(view);
- }
這裏調用inflate的時候,強轉了view爲viewgroup,因爲其本身就是linearlayout,所以這裏可以強轉。
運行的效果如下:
單看效果而言,跟上面的一樣。但從代碼本身而言,實現的內容就不一樣了。由於有了viewgroup,這裏得到的視圖其實已經有了layoutparam,你可以自行打印Log看看。
但爲什麼最後的結果卻是和上面的一樣呢。原因還是由於設置視圖到activity時,會取得當前window的layoutparam賦值給它,也就是充滿全屏。
第三種方式:inflate(view, root, true)
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
- null);
- view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view,
- true);
- setContentView(view);
- }
這個效果就很明顯了,由於main是線性佈局,所以,test視圖被添加到了textview(hello world)下面,並且保留了其自己的layoutparam參數。
例子很簡單,就不附上代碼工程。
如果對inflate方法如何實現的,感興趣的話,可以參考上一篇文章:
Android編程之LayoutInflater的inflate方法詳解
補充:新的API會在inflater.inflate(R.layout.xxx, null);提示錯誤: