一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Android开发CheckBox自定义图片问题

时间:2013-12-19 编辑:简简单单 来源:一聚教程网

一,CheckBox自定义图片问题

结果点击的时候 会有checked效果,但是,手指离开后没有check住~

 代码如下 复制代码

<selector xmlns:android="http://schemas.android.com/apk/res/android">

android:drawable="@drawable/checkbox_tishi" />
android:drawable="@drawable/checkbox_tishi_select" />

上面的写法,最后当然没好使,于是就搜索了一下,搜来的多了一对标签状态,但还是不能用。但是原生的是可以的,想了想还是看看源码好了。发现源码中的配置多了不少,当然首先就是全部复制过来。然后,适当精简一下。再次发布程序,顿时好使了。结果如下:

 代码如下 复制代码

android:drawable="@drawable/checkbox_tishi_select" />
android:drawable="@drawable/checkbox_tishi" />

android:drawable="@drawable/checkbox_tishi_select" />
android:drawable="@drawable/checkbox_tishi" />

android:drawable="@drawable/checkbox_tishi_select" />
android:drawable="@drawable/checkbox_tishi" />

android:drawable="@drawable/checkbox_tishi" />
android:drawable="@drawable/checkbox_tishi_select" />


二,CheckBox自定义图片大小问题


1.在drawable中创建文件checkbox_selector.xml:

 代码如下 复制代码



      android:drawable="@drawable/checkbox_ok" />
     android:drawable="@drawable/checkbox_empty" />
2.在values中创建styles.xml:

   

3.在你的CheckBox中添加属性:

 代码如下 复制代码

        android:id="@+id/check"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="5dp"
        style="@style/MyCheckBox" 
        />

热门栏目