Android Development CheckBox Custom Picture problem

Source: Internet
Author: User

One, checkbox custom picture problem

The results are checked when clicked, but there is no check when the finger is gone.

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Utf-8"?>

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

<item android:state_checked= "false"
android:drawable= "@drawable/checkbox_tishi"/>
<item android:state_checked= "true"
android:drawable= "@drawable/checkbox_tishi_select"/>

</selector>

The above writing, of course, did not work, so the search for a while, search for more than a pair of label status, but still can not be used. But the original is OK, think about it or look at the source of the good. Found in the source of more than a lot of configuration, of course, the first is all copied over. Then, streamline it appropriately. Releasing the program again immediately works. The results are as follows:

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Utf-8"?>

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

<item android:state_checked= "true" android:state_window_focused= "false"
android:drawable= "@drawable/checkbox_tishi_select"/>
<item android:state_checked= "false" android:state_window_focused= "false"
android:drawable= "@drawable/checkbox_tishi"/>

<item android:state_checked= "true" android:state_pressed= "true"
android:drawable= "@drawable/checkbox_tishi_select"/>
<item android:state_checked= "false" android:state_pressed= "true"
android:drawable= "@drawable/checkbox_tishi"/>

<item android:state_checked= "true" android:state_focused= "true"
android:drawable= "@drawable/checkbox_tishi_select"/>
<item android:state_checked= "false" android:state_focused= "true"
android:drawable= "@drawable/checkbox_tishi"/>

<item android:state_checked= "false"
android:drawable= "@drawable/checkbox_tishi"/>
<item android:state_checked= "true"
android:drawable= "@drawable/checkbox_tishi_select"/>

</selector>


Two, checkbox custom picture size problem


1. Create file Checkbox_selector.xml in drawable:

The code is as follows Copy Code

<?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/checkbox_ok"/><!--settings Select a picture-->
<item android:state_checked= "false"
android:drawable= "@drawable/checkbox_empty"/><!--set unselected picture-->
</selector>2. Create Styles.xml in values:

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>

<style name= "MyCheckBox" parent= "@android: Style/widget.compoundbutton.checkbox" >

<item name= "Android:button" > @drawable/checkbox_selector</item>

<item name= "Android:paddingleft" >25.0dip</item>

<item name= "Android:maxheight" >10.0dip</item>

</style>

</resources>

3. Add attributes to your checkbox:

  code is as follows copy code

< CheckBox
        android:id= "@+id/check"
         android:layout_width= "20DP"
        android:layout_ height= "20DP"
        android:layout_marginleft= "5DP"
         style= "@style/mycheckbox"  
       /

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.