android中设置CheckBox的样式

1、设置CheckBox选中/不选中,按下/不按下状态的样式

   <1>、在drawable中配置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:state_pressed="true" android:drawable="@drawable/focused" ;/>
       <item android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/normal" ;/>
       <item android:state_checked="false" android:drawable="@drawable/normal" ;/>
       <item android:state_checked="true" android:drawable="@drawable/focused" ;/>
     </selector>


  
    <2>、在res/values/styles.xml文件中添加MyCheckBox名称的Style

           

 

 

        <style name="MyCheckBox">
          <item name="android:button">@drawable/checkbox</item>
        </style>

 

 <3>、在布局文件中设置CheckBox的style属性,如:
          

 

      <CheckBox android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
      style="@style/MyCheckBox"/>


 

2、当CheckBox作为ListView中ListItem的子组件时,ChekBox要有下面三个配置
 

  

   android:focusable="false"

 因为checkbox的点击事件优先级比listview的高,所以要在checkbox中添加android:focusable="false",使得checkbox初始的时候没有获取焦点。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值