更改复选框默认布局时,android:button不起作用

时间:2019-07-14 18:12:43

标签: android android-layout

我想自定义我要设置按钮并将其作为选择器的复选框。但是该复选框完全消失,没有任何显示。这是代码:

<CheckBox
    android:id="@+id/bollywood_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/relationship"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="5dp"
    android:button="@drawable/check_background" />

我的选择器文件

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

    <item android:drawable="@drawable/bg_comment" android:state_checked="true"/>
    <item android:drawable="@drawable/bg_et"/>

</selector>

1 个答案:

答案 0 :(得分:0)

尝试一下,它对我有用

selector_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_checkbox_on" android:state_checked="true"/>
    <item android:drawable="@drawable/ic_checkbox_off" android:state_checked="false"/>
</selector>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


    <CheckBox
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/bollywood_check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:button="@drawable/selector_checkbox"
            android:text="Hello"
            android:layout_marginTop="5dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>