LayoutInflater.inflate從實驗到源碼分析

實驗

在MainActivity中加載一個子佈局:android

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
    android:id="@+id/weChatPullView"
    android:orientation="vertical"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/loadLayoutBtn"
        android:text="加載佈局"
        android:layout_width="110dp"
        android:layout_height="50dp" />

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </RelativeLayout>
</RelativeLayout>

子佈局:函數

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/testBtn"
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

四種能夠成功加載子佈局的狀況:源碼分析

LayoutInflater inflater = getLayoutInflater();
inflater.inflate(R.layout.test_layout,container,true);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.test_layout,null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                container.addView(view,params);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.test_layout,container,false);
container.addView(view);

4.兩個參數時 , 第三個參數默認是true佈局

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.test_layout,container);

沒法加載子佈局的狀況:code

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.test_layout,null);
container.addView(view);

分析實驗

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

主要分析第二個和第三個參數的做用:xml

  • 從上面的實驗來看,若是沒有第二個參數(爲null),咱們就須要設置要加載的view的寬和高,才能實現成功加載到父佈局中。utf-8

  • 第三個參數爲true的時候,自動加載該view到root佈局中去,不須要調用addView這個函數,說明第三個參數的做用是判斷是否加載佈局到父佈局中去get

源碼分析

if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(params);
                        }
                    }

params = root.generateLayoutParams(attrs); 能夠看出來源碼

若是 root 爲空 ,那麼無論 須要 被加載的view佈局參數 是怎樣的 ,都不能計算出來他的佈局參數,也就沒法經過parent.addView加載到佈局中去it

大部分狀況下,咱們都是使用 inflate(id,container,false) 這種方式去加載子view;DialogFragment不須要container去生成佈局參數,第二個參數爲null

相關文章
相關標籤/搜索