Android LayoutInflater的inflater方法詳解

關於inflate參數問題,我想很多人多多少少都瞭解一點,網上也有很多關於這方面介紹的文章,但是枯燥的理論或者翻譯讓很多小夥伴看完之後還是一臉懵逼,so,我今天想通過三個案例來讓小夥伴徹底的搞清楚這個東東。本篇博客我們不講源碼,只看使用。源碼的解讀會在下一篇博文中帶來。

inflate方法從大範圍來看,分兩種,三個參數的構造方法和兩個參數的構造方法。在這兩類中又有細分,OK,那我們就把各種情況都來演示一遍。

1.三個參數的inflate方法

方法頭如下:

    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)  

好,這裏主要分爲三種情況,分別來看

1.1 root不爲null,attachToRoot爲true

當root不爲null,attachToRoot爲true時,表示將resource指定的佈局添加到root中,添加的過程中resource所指定的的佈局的根節點的各個屬性都是有效的。比如下面一個案例,我的Activity的佈局如下:

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical"  
        android:id="@+id/ll"  
        tools:context="org.sang.layoutinflater.MainActivity">  
    </LinearLayout>  

我還有一個佈局linearlayout.xml如下:

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/ll"  
        android:layout_width="200dp"  
        android:layout_height="200dp"  
        android:background="@color/colorPrimary"  
        android:gravity="center"  
        android:orientation="vertical">  
      
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content" />  
    </LinearLayout>  

我現在想把這個linearlayout.xml佈局文件添加到我的activity的佈局中,那麼我可以這麼做:

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    LinearLayout ll = (LinearLayout) findViewById(R.id.ll);  
    LayoutInflater inflater = LayoutInflater.from(this);  
    inflater.inflate(R.layout.linearlayout, ll,true);  
} 
小夥伴們注意到,這裏我都沒寫將inflate出來的View添加到ll中的代碼,但是linearlayout佈局文件就已經添加進來了,這就是因爲我第三個參數設置爲了true,表示將第一個參數所指定的佈局添加到第二個參數的View中。最終顯示效果如下:

如果我作死多寫這麼一行代碼,如下:

    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);  
        LayoutInflater inflater = LayoutInflater.from(this);  
        View view = inflater.inflate(R.layout.linearlayout, ll, true);  
        ll.addView(view);  
    }  

這個時候再運行,系統會拋如下異常:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.  
原因就是因爲當第三個參數爲true時,會自動將第一個參數所指定的View添加到第二個參數所指定的View中。

1.2 root不爲null,attachToRoot爲false

如果root不爲null,而attachToRoot爲false的話,表示不將第一個參數所指定的View添加到root中,那麼這個時候有的小夥伴可能就有疑問了,既然不添加到root中,那我還寫這麼多幹嘛?我第二個參數直接給null不就可以了?其實不然,這裏涉及到另外一個問題:我們在開發的過程中給控件所指定的layout_width和layout_height到底是什麼意思?該屬性的表示一個控件在容器中的大小,就是說這個控件必須在容器中,這個屬性纔有意義,否則無意義。這就意味着如果我直接將linearlayout加載進來而不給它指定一個父佈局,則inflate佈局的根節點的layout_width和layout_height屬性將會失效(因爲這個時候linearlayout將不處於任何容器中,那麼它的根節點的寬高自然會失效)。如果我想讓linearlayout的根節點有效,又不想讓其處於某一個容器中,那我就可以設置root不爲null,而attachToRoot爲false。這樣,指定root的目的也就很明確了,即root會協助linearlayout的根節點生成佈局參數,只有這一個作用。OK,還是上面的佈局文件,如果我想將之添加到activity的佈局中又該如何呢?

    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);  
        LayoutInflater inflater = LayoutInflater.from(this);  
        View view = inflater.inflate(R.layout.linearlayout, ll, false);  
        ll.addView(view);  
    }  
大家注意,這個時候我需要手動的將inflate加載進來的view添加到ll容器中,因爲inflate的最後一個參數false表示不將linealayout添加到ll中。顯示效果和上文一樣,不再貼圖。

1.3 root爲null

當root爲null時,不論attachToRoot爲true還是爲false,顯示效果都是一樣的。當root爲null表示我不需要將第一個參數所指定的佈局添加到任何容器中,同時也表示沒有任何容器來來協助第一個參數所指定佈局的根節點生成佈局參數。我還是使用上文提到的linearlayout,我們來看下面一段代碼:

    protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            LinearLayout ll = (LinearLayout) findViewById(R.id.ll);  
            LayoutInflater inflater = LayoutInflater.from(this);  
            View view = inflater.inflate(R.layout.linearlayout, null, false);  
            ll.addView(view);  
        }  
當第二個參數爲null,第三個參數爲false時(即使爲true顯示效果也是一樣的,這裏以false爲例),由於在inflate方法中沒有將linearlayout添加到某一個容器中,所以我需要手動添加,另外由於linearlayout並沒有處於某一個容器中,所以它的根節點的寬高屬性會失效,顯示效果如下:


小夥伴們注意,這個時候不管我給linearlayout的根節點的寬高設置什麼,都是沒有效果的,它都是包裹button,如果我修改button,則button會立即有變化,因爲button是處於某一個容器中的。

2.兩個參數的inflate方法

兩個參數的inflate方法就很簡單了,我們來稍微看一點點源碼:
    public View inflate(XmlPullParser parser, @Nullable ViewGroup root) {  
            return inflate(parser, root, root != null);  
        }  

這是兩個參數的inflate方法,大家注意兩個參數實際上最終也是調用了三個參數。
兩個參數的inflate方法分爲如下兩種情況:
1.root爲null,等同於1.3所述情況。
2.root不爲null,等同於1.1所述情況。

3.爲什麼Activity佈局的根節點的寬高屬性會生效?

inflate方法我們已經說完了,小夥伴們可能有另外一個疑問,那爲什麼Activity佈局的根節點的寬高屬性會生效?其實原因很簡單,大部分情況下我們一個Activity頁面由兩部分組成(Android的版本號和應用主題會影響到Activity頁面組成,這裏以常見頁面爲例),我們的頁面中有一個頂級View叫做DecorView,DecorView中包含一個豎直方向的LinearLayout,LinearLayout由兩部分組成,第一部分是標題欄,第二部分是內容欄,內容欄是一個FrameLayout,我們在Activity中調用setContentView就是將View添加到這個FrameLayout中,所以給大家一種錯覺彷彿Activity的根佈局很特殊,其實不然。


OK,以上就是對LayoutInflater中inflate方法的一個簡單介紹,希望能夠幫助到還沒弄懂這個的小夥伴。



文章轉載自:https://blog.csdn.net/u012702547/article/details/52628453



發佈了44 篇原創文章 · 獲贊 7 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章