现在的位置: 首页 > 综合 > 正文

android中设置CheckBox的样式

2013年10月09日 ⁄ 综合 ⁄ 共 1013字 ⁄ 字号 评论关闭

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初始的时候没有获取焦点。

抱歉!评论已关闭.