LayoutInflater.from(context).inflate(R.layout.title,this)的原理詳解

LayoutInflater.from(context).inflate(R.layout.title,this)的原理詳解
原博鏈接https://blog.csdn.net/u012702547/article/details/52628453

inflate方法兩個參數(shù)和三個參數(shù)的區(qū)別

三個參數(shù)的inflate方法:

View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
  1. root不為null,attachToRoot為true,表示將制定的布局添加到root中,添加的過程總resource所指定的布局的根節(jié)點的各個屬性都是有效的看下面一個例子
<?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添加到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布局文件就已經(jīng)添加進來了,這就是因為我第三個參數(shù)設置為了true,表示將第一個參數(shù)所指定的布局添加到第二個參數(shù)的View中。

image

但如果這樣寫的話:

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.

原因就是因為當?shù)谌齻€參數(shù)為true時,會自動將第一個參數(shù)所指定的View添加到第二個參數(shù)所指定的View中。

  1. root不為null,attachToRoot為false
    如果root不為null,而attachToRoot為false的話,表示不將第一個參數(shù)所指定的View添加到root中,那么這個時候有的小伙伴可能就有疑問了,既然不添加到root中,那我還寫這么多干嘛?我第二個參數(shù)直接給null不就可以了?其實不然,這里涉及到另外一個問題:我們在開發(fā)的過程中給控件所指定的layout_width和layout_height到底是什么意思?該屬性的表示一個控件在容器中的大小,就是說這個控件必須在容器中,這個屬性才有意義,否則無意義。這就意味著如果我直接將linearlayout加載進來而不給它指定一個父布局,則inflate布局的根節(jié)點的layout_width和layout_height屬性將會失效(因為這個時候linearlayout將不處于任何容器中,那么它的根節(jié)點的寬高自然會失效)。如果我想讓linearlayout的根節(jié)點有效,又不想讓其處于某一個容器中,那我就可以設置root不為null,而attachToRoot為false。這樣,指定root的目的也就很明確了,即root會協(xié)助linearlayout的根節(jié)點生成布局參數(shù),只有這一個作用。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的最后一個參數(shù)false表示不將linealayout添加到ll中。顯示效果和上文一樣

  1. root為null
    當root為null時,不論attachToRoot為true還是為false,顯示效果都是一樣的。當root為null表示我不需要將第一個參數(shù)所指定的布局添加到任何容器中,同時也表示沒有任何容器來來協(xié)助第一個參數(shù)所指定布局的根節(jié)點生成布局參數(shù)。我還是使用上文提到的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);  
    }  

當?shù)诙€參數(shù)為null,第三個參數(shù)為false時(即使為true顯示效果也是一樣的,這里以false為例),由于在inflate方法中沒有將linearlayout添加到某一個容器中,所以我需要手動添加,另外由于linearlayout并沒有處于某一個容器中,所以它的根節(jié)點的寬高屬性會失效,顯示效果如下:


image

這個時候不管我給linearlayout的根節(jié)點的寬高設置什么,都是沒有效果的,它都是包裹button,如果我修改button,則button會立即有變化,因為button是處于某一個容器中的。

兩個參數(shù)的inflate方法

看源碼:

public View inflate(XmlPullParser parser, @Nullable ViewGroup root) {  
        return inflate(parser, root, root != null);  
    }  

雖然是兩個參數(shù)的方法,但底層實際上等同于調用了帶三個參數(shù)的inflate方法

  • 如果root為null對應的是上述三個參數(shù)inflate方法的第三點
  • 如果root不為null對應的是上述三個參數(shù)inflate方法的第一點

為什么Activity布局的根節(jié)點的寬高屬性會生效?

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

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容