android : clipToPadding clipChildren

1.android:clipToPadding和android:clipChildren是ViewGroup中的属性

2.android:clipToPadding和android:clipChildren默认为true,

3.一定是在布局文件的根节点设置,否则不起作用

setClipChildren

参考:

Android clipToPadding和clipChildren属性使用

默认情况下,子级在绘制之前会被裁剪到其边界。

允许视图组覆盖动画等行为。

    /**
     * By default, children are clipped to their bounds before drawing. This
     * allows view groups to override this behavior for animations, etc.
     *
     * @param clipChildren true to clip children to their bounds,
     *        false otherwise
     * @attr ref android.R.styleable#ViewGroup_clipChildren
     */
    public void setClipChildren(boolean clipChildren) {
        boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN;
        if (clipChildren != previousValue) {
            setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren);
            for (int i = 0; i < mChildrenCount; ++i) {
                View child = getChildAt(i);
                if (child.mRenderNode != null) {
                    child.mRenderNode.setClipToBounds(clipChildren);
                }
            }
            invalidate(true);
        }
    }


demo :


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_dark"
    android:clipChildren="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:layout_gravity="bottom"
            android:layout_weight="1.0"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>
</RelativeLayout>


android:clipChildren=“false”

在这里插入图片描述

android:clipChildren=“true”

在这里插入图片描述

setClipToPadding

参考:

android:clipToPadding的使用详解

android:clipToPadding的使用

设置此ViewGroup是否将其子级剪切到其填充并调整(但不剪切)任何EdgeEffect到填充区域(如果存在填充)。

默认情况下,子级将被剪切到其父级ViewGroup的填充中。仅当填充不为零时,才启用此裁剪行为。

    /**
     * Sets whether this ViewGroup will clip its children to its padding and resize (but not
     * clip) any EdgeEffect to the padded region, if padding is present.
     * <p>
     * By default, children are clipped to the padding of their parent
     * ViewGroup. This clipping behavior is only enabled if padding is non-zero.
     *
     * @param clipToPadding true to clip children to the padding of the group, and resize (but
     *        not clip) any EdgeEffect to the padded region. False otherwise.
     * @attr ref android.R.styleable#ViewGroup_clipToPadding
     */
    public void setClipToPadding(boolean clipToPadding) {
        if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) {
            setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding);
            invalidate(true);
        }
    }

demo :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_dark"
    android:clipToPadding="false"
    android:paddingTop="50dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginBottom="20dp"
            android:background="@color/colorPrimary"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginBottom="20dp"
            android:background="@color/colorPrimary"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginBottom="20dp"
            android:background="@color/colorPrimary"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginBottom="20dp"
            android:background="@color/colorPrimary"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginBottom="20dp"
            android:background="@color/colorPrimary"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginBottom="20dp"
            android:background="@color/colorPrimary"
            android:src="@mipmap/ic_launcher" />

    </LinearLayout>
</ScrollView>


android:clipToPadding=“false” : paddingTop会随子view一起滚动,listview ,recyclerview常用

android:clipToPadding=“true” : paddingTop固定,不会随子view滚动


版权声明:本文为sinat_31057219原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。