简体   繁体   English

自定义 Android 复选框

[英]Customizing Android checkbox

I am trying to partially change the images for Android's checkboxes.我正在尝试部分更改 Android 复选框的图像。 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 .对于第三个复选框,我已将图像btn_check_on.pngbtn_check_label_background.9.png从 Android 的 SDK 复制到项目的res/drawable并将它们重命名为cb_on.pngcb_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尝试在 cb_background.9.png 中重命名您的图像 cb_background.png

.9 means that the image is a nine-patch image which is done to rescale itself automatically. .9 表示图像是一个九块图像,它会自动重新缩放自身。

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)您需要将它们(mdpi、hdpi、ldpi)全部复制到相应的 res 文件夹(drawable-mdpi、drawable-hdpi、drawable-ldpi)

@Jokahero is right about the 9-patch, and @jkhouw1 is right about the resolutions. @Jokahero 对 9 补丁的看法是正确的,@jkhouw1 对决议的看法是正确的。 For checkboxes, you will also need to know about StateListDrawable .对于复选框,您还需要了解StateListDrawable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM