Android:复选框选择器无法正常工作

时间:2016-01-14 10:44:15

标签: android android-layout checkbox android-checkbox android-selector

我正在开发一个应用程序,我在其中使用复选框和应用选择器。我的复选框代码如下:

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0.5"
    android:button="@drawable/custom_checkbox"
    android:clickable="false"
    android:focusable="false"
    android:gravity="center" />

custom_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/checkbox_checked" android:state_checked="true"/>
    <item android:drawable="@drawable/checkbox_checked" android:state_pressed="true"/>
    <item android:drawable="@drawable/checkbox_unchecked"/>

</selector>

checkbox_checked

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:drawable="@drawable/green_tic"/>
    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="@dimen/corner_radius_for_ask_option"/>
            <size
                android:height="20dp"
                android:width="20dp" />
        </shape>
    </item>

</layer-list>

checkbox_unchecked

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <stroke
                android:width="2px"
                android:color="@color/color_light_grey" />

            <corners android:radius="@dimen/corner_radius_for_ask_option" />

            <size
                android:height="20dp"
                android:width="20dp" />
        </shape>
    </item>

</layer-list>

以上代码在Note 3和同类手机上运行正常。它看起来像:

uncheck checked

但是在三星s2上看起来像:

uncheck checked

1 个答案:

答案 0 :(得分:3)

尝试在形状中添加透明实体:

<shape android:shape="rectangle" >
    <corners android:radius="@dimen/corner_radius_for_ask_option"/>
    <size
        android:height="20dp"
        android:width="20dp" />
    <solid android:color="@android:color/transparent"/>
</shape>

其他可绘制XML也是如此。