天天看點

Android程式設計之LayoutInflater的inflate方法執行個體

如果你不關心其内部實作,隻看如何使用的話,直接看這篇即可。

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

用兩個布局檔案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);
    }
           

運作的效果如下:

Android程式設計之LayoutInflater的inflate方法執行個體

這個就很容易了解了,因為我沒有指定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,是以這裡可以強轉。

運作的效果如下:

Android程式設計之LayoutInflater的inflate方法執行個體

單看效果而言,跟上面的一樣。但從代碼本身而言,實作的内容就不一樣了。由于有了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);
    }
           

運作的效果如下:

Android程式設計之LayoutInflater的inflate方法執行個體

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

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

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

Android程式設計之LayoutInflater的inflate方法詳解

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

Android程式設計之LayoutInflater的inflate方法執行個體

請詳見看後面的轉載的文章解釋:

Layout inflation is the term used within the context of Android to indicate when an XML layout resource is parsed and converted into a hierarchy of View objects.