【问题标题】:Checkbox bad display when checked or unchecked选中或未选中时复选框显示错误
【发布时间】:2019-01-30 17:53:33
【问题描述】:

以下布局中的复选框有显示问题。当我尝试选中或取消选中它们时,我得到了这种结果: 有时,我得到一个灰色或一个非常小的复选标记。 此外,如果我在多次单击它后设法选中一个,如果我单击另一个,它将再次播放第一个复选框的动画。在这种情况下,所有复选框看起来都像“链接”。

我删除了两个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'
}

【问题讨论】:

  • 使用android:buttonTint="@color/blue"
  • 你为复选框使用了自定义动画吗?
  • @HemantParmar 除了盒子的边框之外什么都没有改变
  • @fanjavaid 不,甚至没有自定义样式
  • 已解决rc02。

标签: android xml checkbox


【解决方案1】:

更新

这个问题在com.android.support v.28.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>

其他信息:

【讨论】:

    【解决方案2】:

    在您的布局中拖放新的复选框,并将 id 替换为您现有的 id。

    【讨论】:

    • 真的很奇怪,其实我整个app好像都有这个问题
    【解决方案3】:

    像下面这样更改gradel并检查,可能是因为版本。

    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'
    

    【讨论】:

      猜你喜欢
      • 2015-12-06
      • 2011-10-02
      • 1970-01-01
      • 2016-04-20
      • 2014-08-10
      • 2013-01-23
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多