安卓:为什么复选框未选中时不显示空复选框?

4
我在CardView布局文件中编写了复选框代码。CardView的背景是白色的。通常,我认为未选中的复选框是一个黑色方块。但我的布局中没有出现空白复选框。我只看到了白色的CardView背景(截图中的顶部CardView)。当我点击CardView的右侧区域(即格式化复选框代码的区域)时,会出现绿色的复选框(截图中的底部CardView)。我错过了什么吗? enter image description here
布局文件:
<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/background4main"  >

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/singlecard_view1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardCornerRadius="6dp"
    android:orientation="horizontal"
    android:layout_margin="4dp">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:selectableItemBackground"  >

    <TextView
        android:id="@+id/cardBlankText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="todo"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="20sp"  />

    <TextView
        android:id="@+id/cardBlankText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cardBlankText2"
        android:text="note1"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="20sp"  />

    <CheckBox
        android:id="@+id/chkSelected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"  />

    </RelativeLayout>

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

</LinearLayout>

Adapter file:
...
public class ListViewHolder extends RecyclerView.ViewHolder {

    TextView cardBlankText2;
    TextView cardBlankText3;
    CheckBox chkSelected;

    public ListViewHolder(View itemView) {
        super(itemView);

        cardBlankText2 = (TextView)itemView.findViewById(R.id.cardBlankText2);
        cardBlankText3 = (TextView)itemView.findViewById(R.id.cardBlankText3);
        chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);
    }
...
@Override
public void onBindViewHolder(final ListViewHolder holder, final int position) {

        holder.cardBlankText2.setText(dbList.get(position).getTodo());
        holder.cardBlankText3.setText(dbList.get(position).getNote1());
        holder.chkSelected.setChecked(dbList.get(position).isSelected());
        holder.chkSelected.setTag(dbList.get(position));
}

当您取消选中同一复选框时会发生什么?它是否会像第一个屏幕截图一样再次显示? - Saumik Bhattacharya
@SB 是的,当单击已选框以取消选中时,它会再次显示,就像第一个 CardView 一样,其背景是空白白色。 - AJW
你应该检查这个解决方案 [CheckBox] (https://dev59.com/I2sz5IYBdhLWcg3wmpFb)。 - Muhammad Umair
  1. 构建 -> 清理项目。
  2. 重建项目。
- garish
3个回答

7
使用以下代码:
<android.support.v7.widget.AppCompatCheckBox
                android:id="@+id/settings_notification_checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                app:buttonTint="@color/colorPrimary"/>

而不是这样:

<CheckBox
        android:id="@+id/check"
        android:button="@drawable/customdrawablecheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

2

Drawable customcheckbox.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/unchecked_drawable" />
   <item android:state_checked="true" android:drawable="@drawable/checked_drawable" />
   <item android:drawable="@drawable/unchecked_drawable" /> <!-- default state -->
  </selector>

还有您的XML文件:

<CheckBox
android:id="@+id/check"
android:button="@drawable/customdrawablecheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

1
可能的原因:
  • 你的复选框可能是白色的。

  • 它的可见性可能已经消失了。

还要为textview添加toLeftOf属性,以便它们不会与复选框重叠。检查你的主题来查看复选框的颜色。如果这没有帮助,你可以为未选中状态放置drawable ?android:attr/listChoiceIndicatorMultiple


好的,我会尝试。如何检查复选框在我的主题中将显示什么颜色? - AJW
1
@RT 我的主题中的"textColorSecondary"解决了问题。回答已被点赞并接受。干杯。 - AJW

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接