Customizing Android checkbox
I am trying to partially change the images for Android's checkboxes. Following the tutorial here, I have done the following experiment:
<CheckBox
android:checked="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<CheckBox
android:checked="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:button="@android:drawable/btn_check_on"
android:background="@android:drawable/btn_check_label_background"/>
<CheckBox
android:checked="true"
android:layout_width="开发者_运维知识库wrap_content" android:layout_height="wrap_content"
android:button="@drawable/cb_on"
android:background="@drawable/cb_background"/>
For the third checkbox, I have copied the images btn_check_on.png and btn_check_label_background.9.png from Android's SDK to the project's res/drawable and rename them to cb_on.png and cb_background.png. While I expected the three checkboxes to have identical appearances, surprisingly, the third checkbox is larger than the first two. Can anybody explain why? How can I fix this problem?
Try renaming your image cb_background.png in cb_background.9.png
.9 means that the image is a nine-patch image which is done to rescale itself automatically.
I think that why the third image is larger.
For more information about nine-patch: here
My guess would be that you only copied one resolution of the drawables. You need to copy all of them (mdpi, hdpi,ldpi) to the appropriate res folder (drawable-mdpi, drawable-hdpi, drawable-ldpi)
@Jokahero is right about the 9-patch, and @jkhouw1 is right about the resolutions. For checkboxes, you will also need to know about StateListDrawable.
精彩评论