天天看點

LayoutInflater的inflate方法執行個體

其中,main.xml檔案為:

[html]  view plain copy

  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.     <TextView  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="50dp"  
  9.         android:gravity="center"  
  10.         android:text="hello world" />  
  11. </LinearLayout>  

test.xml檔案為:

[html]  view plain copy

  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.     <TextView  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="50dp"  
  10.         android:gravity="center"  
  11.         android:text="test" />  
  12. </LinearLayout>  

在test中設定了其高度為200dp,并且設定了背景顔色。

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

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

[java]  view plain copy

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

運作的效果如下:

LayoutInflater的inflate方法執行個體

這個就很容易了解了,因為我沒有指定ViewGroup root參數,是以,相當于直接加載了test視圖檔案,并傳回。

而它的高度充滿了全屏而不是200dp,因為執行inflate的時候,沒有root參數,則無法為test視圖設定layoutparam參數。那麼為什麼會充滿螢幕而不是沒有顯示呢?是因為我們将其設定視圖到activity時,會取得目前window的layoutparam指派給它,也就是充滿全屏。

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

[html]  view plain copy

LayoutInflater的inflate方法執行個體
LayoutInflater的inflate方法執行個體
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,  
  5.             null);  
  6.     view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view, false);  
  7.     setContentView(view);  
  8. }  

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

運作的效果如下:

LayoutInflater的inflate方法執行個體

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

但為什麼最後的結果卻是和上面的一樣呢。原因還是由于設定視圖到activity時,會取得目前window的layoutparam指派給它,也就是充滿全屏。

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

[html]  view plain copy

LayoutInflater的inflate方法執行個體
LayoutInflater的inflate方法執行個體
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,  
  5.             null);  
  6.     view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view,  
  7.             true);  
  8.     setContentView(view);  
  9. }  

運作的效果如下:

LayoutInflater的inflate方法執行個體

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