Android复选框自定义图像未正确显示



在我的项目中,我有一个复选框,我正在尝试添加一个自定义图像以进行检查和未检查的状态,但是这些图像对xhdpi screen screen不正确显示。

我正在张贴XML和屏幕屏幕

custom_checkbox.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" />

单击此处获取HDPI图像

单击此处获取XHDPI图像

您应该将自定义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>

最新更新