CheckBox 的默認(rèn)效果是文字在右邊,如下所示
但是我們經(jīng)常是有文字在左邊的需求.可以按照下面的方式進(jìn)行處理
<CheckBox
android:text="CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
android:drawableEnd="?android:attr/listChoiceIndicatorMultiple"
android:checked="true"
android:layout_centerInParent="true"
android:layout_marginBottom="32dp" />
重點(diǎn)是 android:button="@null" 和 **android:drawableRight="?android:attr/listChoiceIndicatorMultiple" **的使用drawableRight這里直接是引用了系統(tǒng)自帶的圖片.當(dāng)然大家可以自定義自己樣式,方法如下:
在drawable目錄下新建如下文件,在 android:drawableRight屬性中引用即可
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/abc_btn_check_to_on_mtrl_015" />
<item android:drawable="@drawable/abc_btn_check_to_on_mtrl_000" />
</selector>
但是假使你只是對選中的樣式不滿,而且希望整個App都保持統(tǒng)一的風(fēng)格,對每個CheckBox單獨(dú)設(shè)置同一的style,這樣是可行的的.但是每一個都進(jìn)行設(shè)置有些麻煩,我們可以在 APP 的 theme 中進(jìn)行處理.注意checkboxStyle的使用.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="checkboxStyle">@style/MyCheckBox</item>
</style>
<style name="MyCheckBox" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/my_check_box</item>
<item name="android:background">?attr/controlBackground</item>
</style>
drawable目錄下新建如下文件(這里選中的圖片我還是選擇了系統(tǒng)自帶的一個,大家按需更改)
abc_btn_check_to_on_mtrl_015所對應(yīng)的圖片
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/abc_btn_check_to_on_mtrl_015" />
<item android:drawable="@drawable/abc_btn_check_to_on_mtrl_000" />
</selector>
在工程中應(yīng)用了AppTheme該theme,大家正常寫CheckBox不用設(shè)置樣式,即可有效果,而且是全局統(tǒng)一的.
<CheckBox
android:text="CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:checked="true"
android:layout_centerInParent="true"
android:layout_marginBottom="32dp" />
可以看到我并沒有對樣式有任何的設(shè)置,但是默認(rèn)的效果已經(jīng)更改了.
而且其他的控件也是同理的,比如更改EditText的默認(rèn) hint 色值等.只要在theme中找到控件對應(yīng)的style進(jìn)行一下配置,全局就可以有統(tǒng)一的效果.
我們經(jīng)常有如下的需求:
一般我們都是利用線性布局或者相對布局包裹 TextView 和 ImageView 來完成,其實(shí)用一個 CheckBox (上面提到的文字在左的形式)就可以替代上面三個控件所達(dá)到的效果,而且某種意義上更方便點(diǎn).但是如果你不加任何處理可能在真機(jī)出現(xiàn)如下問題:
水波紋會跑出控件的范圍,模擬器上是沒有這樣的問題,具體原因本人還尚不清楚,若有知道的望告知一下.
解決這個問題的辦法,可以隨便設(shè)置一個背景色或者引用系統(tǒng)的水波紋或者自定義的背景效果.這里引用系統(tǒng)的效果.
android:background="?android:attr/selectableItemBackground"
效果如下:
題外話:截取這些效果圖還真不容易,稍縱即逝.
關(guān)于適配的一個問題,我要在這里提下.避免大家踩坑.如果你想要的效果是選中有類似'勾'號的效果,未選中不出現(xiàn)任何圖標(biāo).下面這種的做法都是不可行的.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/abc_btn_check_to_on_mtrl_015" />
<item android:state_checked="false" android:drawable="@null" /><!--@null或者不寫這句話都是不可行的-->
</selector>
解決的辦法 android:state_checked="false" 狀態(tài)下引用一張'選中效果'同大小的透明圖片或者與背景色相同的圖片.注意必須相同大小,否則的話兩種效果的圖片會以最小圖片的尺寸為標(biāo)準(zhǔn)展示出來,這會影響你的適配效果.