Create custom check boxes in android

依然范特西╮ 提交于 2019-12-21 05:57:36

问题


I have an android application. There is an activity which demands 7 checkboxes but as the size of the default check boxes provided by SDK is very large and takes lots of space, I tried to develop my own customized check boxes.

For this, I have captured two images (checked and unchecked), On click of one it toggles, i.e. on click of checked it converts to unchecked and vice versa.

But I want to know is there any other way to do this..


回答1:


It's little simple by placing an appropriate drawable xml file for your checkbox states with corresponding images you need to set.

place the below code in drawable named 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/star_checked" /> <!-- pressed -->
    <item android:state_checked="false"
          android:drawable="@drawable/star_unchecked" /> <!-- focused -->
     <item android:drawable="@drawable/star_unchecked" />      
</selector>

and on your checkbox set above code to manipulate, by using android:button=@drawable/checkbox"

<CheckBox android:id="@+id/chkFav" android:layout_width="wrap_content"
        android:layout_marginRight="0dp" android:button="@drawable/checkbox"
              android:layout_height="wrap_content" android:clickable="true"/>


来源:https://stackoverflow.com/questions/6353608/create-custom-check-boxes-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!