ClipToPadding & ClipChildren

clipToPadding和clipChidren可以用来改变子视图的样式和行为

clipToPadding

字面意思是根据padding来裁剪视图,如padding_top = 20dp,那么子视图从上边距20dp开始绘制。既然是padding相关,所以一般用在viewGroup里,尤其是可以滚动的recyclerView,scrollView等控制上下边距。

1,clipToPadding=”true”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp"
android:paddingTop="16dp"
android:clipToPadding="true"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical">

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

<Button
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="button1"/>

<Button
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="16dp"
android:text="button2"/>
</LinearLayout>

</ScrollView>

s

2,clipToPadding=”false”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp"
android:paddingTop="16dp"
android:clipToPadding="false"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical">

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

<Button
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="button1"/>

<Button
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="16dp"
android:text="button2"/>
</LinearLayout>

</ScrollView>

s

clipChildren

是否裁剪子视图,默认是true,一般情况子视图都在父视图里展示。也有些特别情况某个视图需要突出来,比如有些floatButton需要突出半边,这时需要设置为false。一般在root view里设置。

1,clipChildren=”true”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical"
android:clipChildren="true"
android:paddingTop="16dp">

<LinearLayout
android:layout_width="200dp"
android:background="@android:color/white"
android:layout_height="100dp"
android:orientation="vertical">

<Button
android:layout_width="600dp"
android:layout_height="300dp"
android:text="button1"/>
</LinearLayout>

</ScrollView>

s

2,clipChildren=”false”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical"
android:clipChildren="false"
android:paddingTop="16dp">

<LinearLayout
android:layout_width="200dp"
android:background="@android:color/white"
android:layout_height="100dp"
android:orientation="vertical">

<Button
android:layout_width="600dp"
android:layout_height="300dp"
android:text="button1"/>
</LinearLayout>

</ScrollView>

s

原理

上面两个属性都在viewGroup,所以直接看源码,可以找到一个mGroupFlags属性,有clipToPadding的赋值,采用的位标记,在事件分发方法dispatchTouchEvent有相应逻辑来判断事件是否响应,在dispatchDraw里判断是否需要绘制。

参考

https://stackoverflow.com/questions/40953049/android-what-does-the-cliptopadding-attribute-do/40955714#40955714