Android CheckBox 自定义图像无法正确显示

标签 android

在我的项目中,我有一个复选框,我正在尝试为选中和未选中状态添加自定义图像,但图像无法正确显示 xhdpi 屏幕。请帮助

我正在发布 xml 和屏幕截图

自定义复选框.xml

 <?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/ic_uncheck" />
       <item android:state_checked="true" android:drawable="@drawable/ic_check" />
       <item android:drawable="@drawable/ic_uncheck" /> <!-- default -->
 </selector>

check_box.xml

     <CheckBox
        android:id="@+id/chkVibrate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:button="@drawable/checkbox"
        android:text="CheckBox Text"
        android:textColor="@color/white" />

click here for the hdpi image

click here for the xhdpi image

最佳答案

您应该为按钮属性使用您的自定义 xml 文件

 <CheckBox
    android:id="@+id/chkVibrate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:button="@drawable/custom_checkbox"
    android:text="CheckBox Text"
    android:textColor="@color/white" />

如果你想在不同的焦点状态下设置不同的图像,你也可以通过这种方式创建一个自定义文件。

<?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/ic_check"
    android:state_focused="false">
</item>
<item android:state_checked="true" 
    android:drawable="@drawable/ic_check"
    android:state_focused="true">
</item>
<item android:state_checked="false" 
    android:drawable="@drawable/ic_uncheck"
    android:state_focused="false">
</item>
<item android:state_checked="false" 
    android:drawable="@drawable/ic_uncheck"
    android:state_focused="true">
</item>
</selector>

关于Android CheckBox 自定义图像无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21494258/

相关文章:

Android-如何等待 AlertDialog 检查?

android - 如何知道最近的iBeacon

java - 从 Android 中的 Openfire 服务器获取所有用户的列表

android - 如何在离线应用程序中使用 mysql?

java - 如何在android中创建字典?

android - 将包从 Android list 移动到构建文件

android - 给出错误 : Too few positional arguments: 1 required, 0

java - 在 Android 应用程序中设置单个静态 applicationContext 有哪些缺点?

android - 如果用户的位置未在 place picker android 中打开,则显示对话框

java - 从编辑文本框Android中删除默认文本