定制知识积累

若对系统自带的widget外观不满意,可以进行定制,原理是修改widget属性对应的drawable,操作步骤如下:

1.在android系统的styles.xml/theme.xml中找到控件的属性所对应的drawable(图片或selector);

2.在项目的styles.xml中自定义风格,继承系统风格,修改相应的drawable;

3.在layout.xml中设置widget的style = "@style/自定义style";

CheckBox定制

  1. 在sdk/platforms/android-**/data/res文件夹内搜索"styles.xml",并打开;
  2. 找到所需控件的style:
     <style name="Widget.CompoundButton.CheckBox">
         <item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
     </style>
    属性开头是"?",表明引用了系统的theme属性;
  3. 继续在当前目录内搜索"theme.xml",打开后找到"listChoiceIndicatorMultiple":
    <item name="listChoiceIndicatorMultiple">@android:drawable/btn_check</item>
  4. 再次搜索"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即可;
  5. 在项目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>
  6. 在项目values/styles.xml中自定义style:
     <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
         <item name="android:button">@drawable/selector_my_checkbox</item>
     </style>
  7. 设置CheckBox的属性即可:
     <CheckBox
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         style="@style/MyCheckBox"/>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值