LayoutInflater的inflate方法

LayoutInflater的inflate方法,在fragment的onCreateView方法中經常用到:

[java] view plaincopy
  1. public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  2.         Bundle savedInstanceState) {  

LayoutInflater的inflate方法一共有四種,但我們日常用經常用到的就只有這兩種:

[java] view plaincopy
  1. public View inflate(int resource, ViewGroup root) {  
  2.     return inflate(resource, root, root != null);  
  3. }  

[java] view plaincopy
  1. public View inflate(int resource, ViewGroup root, boolean attachToRoot) {  
  2.     if (DEBUG) System.out.println("INFLATING from resource: " + resource);  
  3.     XmlResourceParser parser = getContext().getResources().getLayout(resource);  
  4.     try {  
  5.         return inflate(parser, root, attachToRoot);  
  6.     } finally {  
  7.         parser.close();  
  8.     }  
  9. }  


所以,這裏直接介紹裏面調用這個方法即可:

[java] view plaincopy
  1. public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)  

在這個方法裏面,上半部分爲xml解析的代碼,這裏就不貼出來,確實沒什麼東西可看。直接看中間部分的代碼:

[java] view plaincopy
  1. ViewGroup.LayoutParams params = null;  
  2.   
  3. if (root != null) {  
  4.     if (DEBUG) {  
  5.         System.out.println("Creating params from root: " +  
  6.                 root);  
  7.     }  
  8.     // Create layout params that match root, if supplied  
  9.     params = root.generateLayoutParams(attrs);  
  10.     if (!attachToRoot) {  
  11.         // Set the layout params for temp if we are not  
  12.         // attaching. (If we are, we use addView, below)  
  13.         temp.setLayoutParams(params);  
  14.     }  
  15. }  


params = root.generateLayoutParams(attrs);

這段的意思是:如果調用inflate方法,傳入了ViewGroup root參數,則會從root中得到由layout_width和layout_height組成的LayoutParams,在attachToRoot設置爲false的話,就會對我們加載的視圖View設置該LayoutParams。

接着往下看:

[java] view plaincopy
  1. // We are supposed to attach all the views we found (int temp)  
  2. // to root. Do that now.  
  3. if (root != null && attachToRoot) {  
  4.     root.addView(temp, params);  
  5. }  
  6.   
  7. // Decide whether to return the root that was passed in or the  
  8. // top view found in xml.  
  9. if (root == null || !attachToRoot) {  
  10.     result = temp;  
  11. }  


root.addView(temp, params);

如果設置了ViewGroup root參數,且attachToRoot設置爲true的話,則將我們加載的視圖做爲子視圖添加到root視圖中。


如果我們ViewGroup root設置爲空的話,就直接返回我們創建的視圖;如果root不爲空,且attachToRoot設置爲false的話,就返回上面那段:對我們加載的視圖View設置該LayoutParams。




接上篇,接下來,就用最最簡單的例子來說明一下:

用兩個佈局文件main 和 test:

其中,main.xml文件爲:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="50dp"  
  10.         android:gravity="center"  
  11.         android:text="hello world" />  
  12.   
  13. </LinearLayout>  

test.xml文件爲:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="200dp"  
  5.     android:background="#ffffff00"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="50dp"  
  11.         android:gravity="center"  
  12.         android:text="test" />  
  13.   
  14. </LinearLayout>  

在test中設置了其高度爲200dp,並且設置了背景顏色。


接下來看一下LayoutInflater().inflate方法實現:

第一種方式:inflate(view, null)

[java] view plaincopy
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,  
  5.             null);  
  6.   
  7.     view = getLayoutInflater().inflate(R.layout.test, null);  
  8.   
  9.     setContentView(view);  
  10. }  

運行的效果如下:


這個就很容易理解了,因爲我沒有指定ViewGroup root參數,所以,相當於直接加載了test視圖文件,並返回。

而它的高度充滿了全屏而不是200dp,因爲執行inflate的時候,沒有root參數,則無法爲test視圖設定layoutparam參數。那麼爲什麼會充滿屏幕而不是沒有顯示呢?是因爲我們將其設置視圖到activity時,會取得當前window的layoutparam賦值給它,也就是充滿全屏。有興趣的話,你可以改一下test的layout_width設定一個數值,最後運行效果是一樣的。


第二種方式:inflate(view, root, false)

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,  
  5.             null);  
  6.   
  7.     view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view, false);  
  8.   
  9.     setContentView(view);  
  10. }  

這裏調用inflate的時候,強轉了view爲viewgroup,因爲其本身就是linearlayout,所以這裏可以強轉。

運行的效果如下:

單看效果而言,跟上面的一樣。但從代碼本身而言,實現的內容就不一樣了。由於有了viewgroup,這裏得到的視圖其實已經有了layoutparam,你可以自行打印Log看看。

但爲什麼最後的結果卻是和上面的一樣呢。原因還是由於設置視圖到activity時,會取得當前window的layoutparam賦值給它,也就是充滿全屏。


第三種方式:inflate(view, root, true)

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,  
  5.             null);  
  6.   
  7.     view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view,  
  8.             true);  
  9.   
  10.     setContentView(view);  
  11. }  
運行的效果如下:

這個效果就很明顯了,由於main是線性佈局,所以,test視圖被添加到了textview(hello world)下面,並且保留了其自己的layoutparam參數。


例子很簡單,就不附上代碼工程。


如果對inflate方法如何實現的,感興趣的話,可以參考上一篇文章:

Android編程之LayoutInflater的inflate方法詳解


補充:新的API會在inflater.inflate(R.layout.xxx, null);提示錯誤:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章