ViewGroup中clipChildren屬性的用法

來(lái)自ViewGroup中的屬性clipChildren用于定義一個(gè)子元素是否被限制在其父元素中進(jìn)行繪制。通常用于動(dòng)畫(huà)效果中繪制需要超出原有尺寸限制的元素時(shí)使用。在這種情況下,需要將該屬性值設(shè)置為false以確保該元素可以超出邊界。缺省值為true,也即子元素不可以超出父元素的邊界。

需要重點(diǎn)注意的是,屬性值clipChildren需要被設(shè)置到爺爺節(jié)點(diǎn)上。

以下為布局的示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:clipChildren="false"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">

        <ImageView
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <ImageView
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <ImageView
             android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_height="96dp"
            android:layout_gravity="bottom"
            android:layout_weight="2"/>

        <ImageView
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <ImageView
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

    </LinearLayout>

</LinearLayout>

重點(diǎn)關(guān)注根節(jié)點(diǎn)的屬性android:clipChildren="false"和第三個(gè)ImageView的屬性android:layout_gravity="bottom",最終效果如下所示:

clipChildren效果

而去除第三個(gè)ImageView的屬性android:layout_gravity="bottom"后的效果為:

layout_gravity未設(shè)置

可以看到,默認(rèn)頂部對(duì)齊,最終只顯示了一部分。

只去除根節(jié)點(diǎn)的屬性android:clipChildren="false"后顯示如下:

clipChildren未設(shè)置

三張圖片對(duì)比,可以很容易看出該屬性值的作用。

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

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