安卓——自定義checkbox圖示
阿新 • • 發佈:2019-02-08
通過自定義checkbox和點選響應事件說說<selector>
1.首先在drawable資料夾中新增checkbox_style.xml
<pre class="html" name="code"><span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"></span>
<span style="font-size:14px;"> <item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/> <item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/> <item android:drawable="@drawable/checkbox_normal"/></span>
<span style="font-size:14px;">
</selector></span>
2.在values資料夾下的styles.xml檔案中新增CustomCheckboxTheme樣式。
<pre class="html" name="code"><span style="font-size:14px;"><style name="</span><span style="font-size:14px;color:#ff0000;">CustomCheckboxTheme</span><span style="font-size:14px;">" parent="@android:style/Widget.CompoundButton.CheckBox"> <item name="android:button">@drawable/</span><span style="font-size:14px;color:#3333ff;">checkbox_style</span><span style="font-size:14px;"></item> </style> </span>
<span style="font-size:18px;">3.在佈局檔案中使用</span><span style="font-size:18px;color:#ff0000;">CustomCheckboxTheme</span><span style="font-size:18px;">樣式。</span>
<span id="_xhe_cursor"><span style="font-size:18px;"></span>
</span>
<span style="font-size:14px;"><CheckBox android:id="@+id/select_all" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/</span><span style="font-size:14px;color:#ff0000;">CustomCheckboxTheme</span><span style="font-size:14px;">" /> </span><span style="font-size:18px;"> </span>
通過查閱總結,首先android的selector是在drawable/xxx.xml中配置的,常用屬性有:
android:state_selected 選中
android:state_focused 獲得焦點
android:state_pressed 點選
android:state_enabled 設定是否響應事件,指所有事件
根據這些狀態同樣可以設定button的selector效果。也可以設定selector改變button中的文字狀態。
以下就是配置button中的文字效果:drawable/button_font.xml<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#FFF" />
<item android:state_focused="true" android:color="#FFF" />
<item android:state_pressed="true" android:color="#FFF" />
<item android:color="#000" />
</selector>
Button還可以實現更復雜的效果,例如漸變啊等等。drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<!-- 定義當button 處於pressed 狀態時的形態。-->
<shape>
<gradient android:startColor="#8600ff" />
<stroke android:width="2dp" android:color="#000000" />
<corners android:radius="5dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp" />
</shape>
</item>
<item android:state_focused="true">
<!-- 定義當button獲得focus時的形態-->
<shape>
<gradient android:startColor="#eac100" />
<stroke android:width="2dp" android:color="#333333" color="#ffffff" />
<corners android:radius="8dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp" />
</shape>
</item>
</selector>
最後,需要在包含button的xml檔案裡新增兩項。假如是main.xml 檔案,我們需要在<Button />里加兩項。
android:focusable="true"
android:backgroud="@drawable/button_color"
這樣當你使用Button的時候就可以甩掉系統自帶的那黃顏色的背景了,實現個性化的背景。