天天看點

【自定義View】LayoutInflater原理分析

相信接觸Android久一點的朋友對于LayoutInflater一定不會陌生,都會知道它主要是用于加載布局的。而剛接觸Android的朋友可能對LayoutInflater不怎麼熟悉,因為加載布局的任務通常都是在Activity中調用setContentView()方法來完成的。其實setContentView()方法的内部也是使用LayoutInflater來加載布局的,隻不過這部分源碼是internal的,不太容易檢視到。那麼今天我們就來把LayoutInflater的工作流程仔細地剖析一遍,也許還能解決掉某些困擾你心頭多年的疑惑

LayoutInflater技術廣泛應用于需要動态添加View的時候,比如在ScrollView和ListView中,經常都可以看到LayoutInflater的身影

LayoutInflater的基本用法,首先需要擷取到LayoutInflater的執行個體,有兩種方法可以擷取到,第一種寫法如下:

LayoutInflater layoutInflater = LayoutInflater.from(context);      

還有另一種寫法:

LayoutInflater layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);      

得到了LayoutInflater的執行個體之後就可以調用它的inflate()方法來加載布局了:

layoutInflater.inflate(resourceId, root);      

inflate()方法一般接收兩個參數,第一個參數就是要加載的布局id,第二個參數是指給該布局的外部再嵌套一層父布局,如果不需要就直接傳null。這樣就成功成功建立了一個布局的執行個體,之後再将它添加到指定的位置就可以顯示出來了

栗子

通過LayoutInflater來将button_layout這個布局添加到主布局檔案的LinearLayout中

比如說目前有一個項目,其中MainActivity對應的布局檔案叫做activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

</LinearLayout>      

我們再定義一個布局檔案,給它取名為button_layout.xml

<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" >

</Button>      

MainActivity

public class MainActivity extends AppCompatActivity {
    private LinearLayout mainLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainLayout = findViewById(R.id.main_layout);
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View view = layoutInflater.inflate(R.layout.button_layout,null);
        mainLayout.addView(view);
    }
}      

運作程式

【自定義View】LayoutInflater原理分析

我們确實是借助LayoutInflater成功将button_layout這個布局添加到LinearLayout中了

如果我們将按鈕的寬度改成300dp,高度改成80dp,這樣夠大了吧?現在重新運作一下程式來觀察效果。沒有任何變化!

其實這裡不管你将Button的layout_width和layout_height的值修改成多少,都不會有任何效果的,因為這兩個值現在已經完全失去了作用

平時我們經常使用layout_width和layout_height來設定View的大小,并且一直都能正常工作,就好像這兩個屬性确實是用于設定View的大小的。而實際上則不然,它們其實是用于設定View在布局中的大小的,也就是說,首先View必須存在于一個布局中,之後如果将layout_width設定成match_parent表示讓View的寬度填充滿布局,如果設定成wrap_content表示讓View的寬度剛好可以包含其内容,如果設定成具體的數值則View的寬度會變成相應的數值。這也是為什麼這兩個屬性叫作layout_width和layout_height,而不是width和height

再來看一下button_layout.xml,很明顯Button這個控件目前不存在于任何布局當中,是以layout_width和layout_height這兩個屬性理所當然沒有任何作用

那麼怎樣修改才能讓按鈕的大小改變呢?解決方法其實有很多種,最簡單的方式就是在Button的外面再嵌套一層布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:text="Button" >
    </Button>

</RelativeLayout>      

可以看到,這裡我們又加入了一個RelativeLayout,此時的Button存在于RelativeLayout之中,layout_width和layout_height屬性也就有作用了。當然,處于最外層的RelativeLayout,它的layout_width和layout_height則會失去作用。現在重新運作一下程式

【自定義View】LayoutInflater原理分析

看到這裡,也許有些朋友心中會有一個巨大的疑惑。平時在Activity中指定布局檔案的時候,最外層的那個布局是可以指定大小的呀,layout_width和layout_height都是有作用的

确實,這主要是因為,在setContentView()方法中,Android會自動在布局檔案的最外層再嵌套一個FrameLayout,是以layout_width和layout_height屬性才會有效果。那麼我們來證明一下吧,修改MainActivity中的代碼

public class MainActivity extends AppCompatActivity {
    private LinearLayout mainLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainLayout = findViewById(R.id.main_layout);
        ViewParent viewParent = mainLayout.getParent();
        Log.d("TAG", "the parent of mainLayout is " + viewParent);
    }
}      

這裡通過findViewById()方法,拿到了activity_main布局中最外層的LinearLayout對象,然後調用它的getParent()方法擷取它的父布局,再通過Log列印出來。現在重新運作一下程式,結果如下圖所示:

【自定義View】LayoutInflater原理分析

LinearLayout的父布局确實是一個FrameLayout,而這個FrameLayout就是由系統自動幫我們添加上的

說到這裡,雖然setContentView()方法大家都會用,但實際上Android界面顯示的原理要比我們所看到的東西複雜得多。任何一個Activity中顯示的界面其實主要都由兩部分組成,标題欄和内容布局。标題欄就是在很多界面頂部顯示的那部分内容,比如剛剛我們的那個例子當中就有标題欄,可以在代碼中控制讓它是否顯示。而内容布局就是一個FrameLayout,這個布局的id叫作content,我們調用setContentView()方法時所傳入的布局其實就是放到這個FrameLayout中的,這也是為什麼這個方法名叫作setContentView(),而不是叫setView()

最後再附上一張Activity視窗的組成圖吧,以便于大家更加直覺地了解:

【自定義View】LayoutInflater原理分析