选中或未选中复选框时显示错误

时间:2018-08-24 09:19:13

标签: android xml checkbox

以下布局中的复选框出现显示问题。当我尝试检查或取消选中它们时,会得到以下结果: Result of trying to check one of the checkboxes 有时,我会看到灰色或很小的复选标记。 此外,如果我在单击几次后设法检查一个,则如果单击另一个,它将再次播放第一个复选框的动画。在这种情况下,所有复选框都看起来像“已链接”。

我同时删除了两个animateLayoutChanges,但它没有任何改变。 我删除了scrollView,但没有结果。 我还在Android 28和23上测试了此布局,并且它仅在API 23上不起作用。

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NewOpeningHourActivity"
    android:animateLayoutChanges="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar_new_opening_hour"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.PopupOverlay"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_new_opening_hour"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="1"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<ScrollView
    android:id="@+id/scroll_new_opening_hour"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/appbar_new_opening_hour"
    app:layout_constraintVertical_bias="0.0">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.CardView
            android:id="@+id/cv_new_opening_hour"
            style="@style/CardFoldAppearance"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:animateLayoutChanges="true"
            android:padding="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <android.support.constraint.ConstraintLayout
                android:id="@+id/ctl_new_opening_hour"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp">

                <TextView
                    android:id="@+id/tv_new_opening_hour_title_days"
                    style="@style/boldMTextAppearance"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="24dp"
                    android:text="@string/new_openhours_card_days_title"
                    app:layout_constraintEnd_toStartOf="@+id/imagebtn_new_opening_hour_days_fold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <ImageButton
                    android:id="@+id/imagebtn_new_opening_hour_days_fold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:contentDescription="Fold or unfold days list"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/ic_round_keyboard_arrow_up_24px" />

                <CheckBox
                    android:id="@+id/cbx_new_opening_hour_day_monday"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:text="@string/day_monday"
                    app:layout_constraintTop_toBottomOf="@+id/tv_new_opening_hour_title_days" />

                <CheckBox
                    android:id="@+id/cbx_new_opening_hour_day_tuesday"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/day_tuesday"
                    app:layout_constraintTop_toBottomOf="@+id/cbx_new_opening_hour_day_monday" />

                <CheckBox
                    android:id="@+id/cbx_new_opening_hour_day_wednesday"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/day_wednesday"
                    app:layout_constraintTop_toBottomOf="@+id/cbx_new_opening_hour_day_tuesday" />

                <CheckBox
                    android:id="@+id/cbx_new_opening_hour_day_thursday"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/day_thursday"
                    app:layout_constraintTop_toBottomOf="@+id/cbx_new_opening_hour_day_wednesday" />

                <CheckBox
                    android:id="@+id/cbx_new_opening_hour_day_friday"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/day_friday"
                    app:layout_constraintTop_toBottomOf="@+id/cbx_new_opening_hour_day_thursday" />

                <CheckBox
                    android:id="@+id/cbx_new_opening_hour_day_saturday"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/day_saturday"
                    app:layout_constraintTop_toBottomOf="@+id/cbx_new_opening_hour_day_friday" />

                <CheckBox
                    android:id="@+id/cbx_new_opening_hour_day_sunday"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/day_sunday"
                    app:layout_constraintTop_toBottomOf="@+id/cbx_new_opening_hour_day_saturday" />


            </android.support.constraint.ConstraintLayout>

        </android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>

</ScrollView>

</android.support.constraint.ConstraintLayout>

我的依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
    implementation 'com.android.support:support-v4:28.0.0-rc01'
    implementation 'com.android.support:design:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

3 个答案:

答案 0 :(得分:1)

更新

此问题已在com.android.support28.0.0版中得到了解决,但是如果您不想升级,可以查看下面的解决方案。


由于我无法解决问题,因此我为复选框创建了自己的选择器。

1。在res/drawable/cbx_selector中:

<?xml version="1.0" encoding="utf-8"?> <!--Used for cbx check problem-->
<!--layer-list needed for paddings-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Initial state-->
    <item android:drawable="@drawable/ic_baseline_check_box_outline_blank_24px"
        android:bottom="4dp"
        android:left="4dp"
        android:right="4dp"
        android:top="4dp"/>

    <!--Paddings to look similar to default Android checkboxes-->
    <item
        android:bottom="4dp"
        android:left="4dp"
        android:right="4dp"
        android:top="4dp">

    <selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:enterFadeDuration="@android:integer/config_shortAnimTime"
        android:exitFadeDuration="@android:integer/config_shortAnimTime"> <!--Used for animation-->

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

        <item android:state_checked="false" android:drawable="@drawable/ic_baseline_check_box_outline_blank_24px"/>

    </selector>
    </item>
</layer-list>

2。我的复选框可绘制对象:

->状态已检查:res/drawable/ic_baseline_check_box_24px

<vector android:autoMirrored="true" android:height="24dp"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"
    android:tintMode="multiply"
    android:tint="@color/cbx_color_checked_selector">
    <!--fillColor MUST be set to white-->
    <path android:fillColor="@android:color/white" android:pathData="M19,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.11,0 2,-0.9 2,-2L21,5c0,-1.1 -0.89,-2 -2,-2zM10,17l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z"/>
</vector>

->未选中状态:res/drawable/ic_baseline_check_box_outline_blank_24px

<vector android:autoMirrored="true" android:height="24dp"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"
    android:tintMode="multiply"
    android:tint="@color/cbx_color_checked_selector">
    <!--fillColor MUST be set to white-->
    <path android:fillColor="@android:color/white" android:pathData="M19,5v14H5V5h14m0,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z"/>
</vector>

3。在res/color/cbx_color_checked_selector中:

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

        <item android:color="@color/colorAccent" android:state_checked="true" android:state_enabled="true" />
        <item android:color="@color/cbxBorderColorUnchecked" android:state_checked="false" android:state_enabled="true" />
        <item android:color="@color/colorAccentDisabled" android:state_checked="true" android:state_enabled="false" />
        <item android:color="@color/cbxBorderColorUncheckedDisabled" android:state_checked="false" android:state_enabled="false" />

    </selector>

其他信息:

答案 1 :(得分:0)

在布局中拖放新的复选框,然后用现有ID替换ID。

答案 2 :(得分:0)

像下面那样更改等级并检查,可能是因为版本。

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'