? ? ?我們在開發(fā)中,常常遇見一些控件都需要自己親手定義樣式!今天帶來的就是一個(gè)比較簡單的復(fù)選按鈕AppCompatCheckBox(v7)
先看看最終實(shí)現(xiàn)的樣式:
看著這樣式,是不是感覺特別清爽,遠(yuǎn)遠(yuǎn)比默認(rèn)的好看多了!下面我們實(shí)現(xiàn)這種效果的控件
首先需要定義1個(gè)按鈕的樣式文件,1個(gè)文字的樣式文件
按鈕的樣式:checkbox_yellow_gray.xml
<selector ? xmlns:android="http://schemas.android.com/apk/res/android">
? ? ? ?<item ?android:drawable="@drawable/xuanze_yuan"
? ? ? ? ? ? ? ? ? android:state_checked="true"/>
? ? ? <itemandroid:drawable="@drawable/yellow_kong"/>
</selector>
文字樣式:text_radiobutton_yellow_gray.xml
<selector ?xmlns:android="http://schemas.android.com/apk/res/android">
? ? ? ? <itemandroid:color="@color/yellow"android:state_checked="true"/>
? ? ? ? <itemandroid:color="@color/contents_hide_color"/>
</selector>
完成這兩個(gè)自定義的樣式后,就需要在布局文件中引用.
main.xml
<android.support.v7.widget.AppCompatCheckBox
? ? ?android:layout_width="wrap_content"
? ? ?android:layout_height="wrap_content"
? ? ?app:backgroundTint="@color/yellow"
? ? ?android:button="@drawable/checkbox_yellow_gray"
? ? ?android:id="@+id/rb_choose_address"
? ? ?android:text="@string/default_address"
? ? ?android:textSize="14sp"
? ? android:padding="5dp"
? ? android:textColor="@drawable/text_radiobutton_yellow_gray"
? ? android:layout_alignBaseline="@+id/btn_update"/>
就這樣幾步,我們就實(shí)現(xiàn)自定義的AppCompatCheckBox(v7)的樣式復(fù)選按鈕,是不是非常簡單!