成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來到九壹網(wǎng)。
搜索
您的當(dāng)前位置:首頁LayoutInflater中inflate方法兩個參數(shù)和三個參數(shù)的區(qū)別

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

來源:九壹網(wǎng)
inflate方法從大范圍來看,分兩種,三個參數(shù)的構(gòu)造方法和兩個參數(shù)的構(gòu)造方法


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


public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) 復(fù)制代碼

那么他們的區(qū)別是什么??root 是否傳值為 null 或者?attachToRoot 等于ture和false的區(qū)別,下面我們就來試一試

一 第一種構(gòu)造方法

1 root不為null,attachToRoot為true


activity_main

<?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>

復(fù)制代碼


linlayout

<?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>

復(fù)制代碼

把這個linlayout.xml布局文件添加到我的activity的布局中,那么我可以這么做:

?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)添加進(jìn)來了,這就是因為我第三個參數(shù)設(shè)置為了true,表示將第一個參數(shù)所指定的布局添加到第二個參數(shù)的View中。最終顯示效果如下:



如果此時我們再加上一句,就會拋出以下異常,因為這個view早已被添加進(jìn)來了,再次添加就會拋出異常

?ll.addView(view)

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


root不為null,attachToRoot為false

setContentView(R.layout.activity_main);

LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

LayoutInflater inflater = LayoutInflater.from(this);

inflater.inflate(R.layout.linearlayout, ll,false);?

?ll.addView(view)


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



我們發(fā)現(xiàn)和第一種效果是一樣的,因為我們手動?ll.addView(view) 把布局添加了進(jìn)來,同時root不為空是又約束了?view 的寬高


3 root為null,attachToRoot為false

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

?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);



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

二 第二種構(gòu)造方法

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
    return inflate(resource, root, root != null);
}復(fù)制代碼

當(dāng)使用第二種構(gòu)造方法時,他也會調(diào)用第一種方法

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)復(fù)制代碼


當(dāng)我們沒有傳遞attachToRoot 時 默認(rèn)為true

LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.linlayout, ll);復(fù)制代碼

效果如圖



轉(zhuǎn)載于:https://juejin.im/post/5d073f79f265da1ba9157df2

因篇幅問題不能全部顯示,請點此查看更多更全內(nèi)容

Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號-2

違法及侵權(quán)請聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市萬商天勤律師事務(wù)所王興未律師提供法律服務(wù)