介绍
查看源码,CheckedTextView
实际上就是一个可以接受选中的 TextView
,并且能自定义更换选中的图标.
1 2 3 4 5 6 7 8 9
| public class CheckedTextView extends TextView implements Checkable { private boolean mChecked; ... private Drawable mCheckMarkDrawable; public void setCheckMarkDrawable(@Nullable Drawable d){...} ... }
|
多种样式
系统自带三种样式:
1 2 3 4 5 6 7 8
| // 多选模式 android:checkMark="?android:attr/listChoiceIndicatorMultiple" // 单选模式 android:checkMark="?android:attr/listChoiceIndicatorSingle" // 复选标记,图标是一个对勾 android:checkMark="?android:attr/textCheckMark" // 垂直居中 android:gravity="center_vertical"
|
示例
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <CheckedTextView android:id="@+id/ctv_single_choice" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="16dp" android:checkMark="?android:attr/listChoiceIndicatorSingle" android:checked="false" android:gravity="center_vertical" android:text="@string/checked_text_single_choice" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
<CheckedTextView android:id="@+id/ctv_multiple_choice" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="16dp" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:gravity="center_vertical" android:text="@string/checked_text_multiple_choice" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/ctv_single_choice" />
<CheckedTextView android:id="@+id/ctv_check_mark" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="16dp" android:checkMark="?android:attr/textCheckMark" android:gravity="center_vertical" android:text="@string/checked_text_check_mark" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/ctv_multiple_choice" />
|