生活随笔
收集整理的這篇文章主要介紹了
定制知识积累
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
若對系統自帶的widget外觀不滿意,可以進行定制,原理是修改widget屬性對應的drawable,操作步驟如下:
1.在android系統的styles.xml/theme.xml中找到控件的屬性所對應的drawable(圖片或selector);
2.在項目的styles.xml中自定義風格,繼承系統風格,修改相應的drawable;
3.在layout.xml中設置widget的style = "@style/自定義style";
CheckBox定制
在sdk/platforms/android-**/data/res文件夾內搜索"styles.xml",并打開;找到所需控件的style:
<style name="Widget.CompoundButton.CheckBox"><item name="android:button">?android:attr/listChoiceIndicatorMultiple</item></style>屬性開頭是"?",表明引用了系統的theme屬性;繼續在當前目錄內搜索"theme.xml",打開后找到"listChoiceIndicatorMultiple":
<item name="listChoiceIndicatorMultiple">@android:drawable/btn_check</item>再次搜索"btn_check",可以找到"btn_check.xml":<selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- Enabled states --><item android:state_checked="true" android:state_window_focused="false"android:state_enabled="true"android:drawable="@drawable/btn_check_on" /><item android:state_checked="false" android:state_window_focused="false"android:state_enabled="true"android:drawable="@drawable/btn_check_off" /><item android:state_checked="true" android:state_pressed="true"android:state_enabled="true"android:drawable="@drawable/btn_check_on_pressed" /><item android:state_checked="false" android:state_pressed="true"android:state_enabled="true"android:drawable="@drawable/btn_check_off_pressed" /><item android:state_checked="true" android:state_focused="true"android:state_enabled="true"android:drawable="@drawable/btn_check_on_selected" /><item android:state_checked="false" android:state_focused="true"android:state_enabled="true"android:drawable="@drawable/btn_check_off_selected" /><item android:state_checked="false"android:state_enabled="true"android:drawable="@drawable/btn_check_off" /><item android:state_checked="true"android:state_enabled="true"android:drawable="@drawable/btn_check_on" /><!-- Disabled states --><item android:state_checked="true" android:state_window_focused="false"android:drawable="@drawable/btn_check_on_disable" /><item android:state_checked="false" android:state_window_focused="false"android:drawable="@drawable/btn_check_off_disable" /><item android:state_checked="true" android:state_focused="true"android:drawable="@drawable/btn_check_on_disable_focused" /><item android:state_checked="false" android:state_focused="true"android:drawable="@drawable/btn_check_off_disable_focused" /><item android:state_checked="false" android:drawable="@drawable/btn_check_off_disable" /><item android:state_checked="true" android:drawable="@drawable/btn_check_on_disable" /></selector>可知系統定義了該widget選中/未選中時的圖片,因此定制時,通過創建style,繼承系統的checkbox風格,引用自定義selector即可;在項目drawable目錄下創建"selector_my_checkbox.xml":
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true" android:drawable="@drawable/my_checkbox_selected" ></item><item android:drawable="@drawable/my_checkbox_unselected"></item>
</selector>在項目values/styles.xml中自定義style:
<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox"><item name="android:button">@drawable/selector_my_checkbox</item></style>設置CheckBox的屬性即可:
<CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/MyCheckBox"/>
轉載于:https://www.cnblogs.com/lynxz0620/p/4384928.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的定制知识积累的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。