1

我正在尝试更改 a 的颜色,CardView具体取决于是否选择了

我试过了,这是我的 card_view_color.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:color="#ABEBC6"/> <!-- selected -->
    <item android:state_selected="false"
        android:color="#FFFFFF"/> <!-- not selected -->
</selector>

这是我的CardView

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardBackgroundColor="@color/card_view_color"
        android:id="@+id/cardViewMetric">
</androidx.cardview.widget.CardView>

也试过了android:state_activatedandroid:state_checked结果是一样的,没用。

我也有一个onClickListener(Kotlin):

holder.binding.root.setOnClickListener{
            val GREEN_COLOR = Color.parseColor("#ABEBC6") 
            holder.binding.cardViewMetric.setCardBackgroundColor(GREEN_COLOR)
        }

这具有当前的行为。
这个

我想要的想要的行为是只有一个 CardView 会被着色,如果我选择另一个,那么一个会是绿色的,而之前的选择会是白色的。

4

2 回答 2

0

您可以MaterialCardView在材料组件库中使用。

<com.google.android.material.card.MaterialCardView
    ...
    app:checkedIcon="@null"
    android:clickable="true"
    android:focusable="true"
    android:checkable="true">
    
</com.google.android.material.card.MaterialCardView>

然后在您的代码中:

    card.setChecked(true)
    

MaterialCardView扩展和androidx.cardview.widget.CardView支持checking状态。

要自定义颜色,您可以使用该app:cardForegroundColor属性:

    <com.google.android.material.card.MaterialCardView
        app:cardForegroundColor="@color/card_selector"

和:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:alpha="...."  android:color="@color/..." android:state_checked="true"/> 
    <item android:alpha="...."  android:color="@color/..." app:state_dragged="true"/>
    <item android:color="@android:color/transparent" android:state_checked="false" app:state_dragged="false"/>
</selector>

或者您可以覆盖colorPrimary卡中的:

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/card"
        style="@style/Widget.MaterialComponents.CardView"

和:

<style name="ThemeOverlay.CardView" parent="">
    <item name="colorPrimary">@color/....</item>
</style>

默认情况下,选中的卡片右上角还有一个选中的图标。您可以使用app:checkedIcon属性对其进行自定义,也可以使用隐藏它app:checkedIcon="@null"

在此处输入图像描述

于 2020-07-16T21:49:09.913 回答
0

感谢Gabriele Mariotti的回答,它给了我一个想法:

我将 CardView 更改为:

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardBackgroundColor="@color/card_view_color"
        android:focusableInTouchMode="true"
        android:id="@+id/cardViewMetric">
    </androidx.cardview.widget.CardView>

(添加了 focusableInTouchMode 属性)

在我的 card_view_color.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"
        android:color="#ABEBC6"/> <!-- selected -->
    <item android:state_focused="false"
        android:color="#FFFFFF"/> <!-- not selected -->
</selector>

现在它有了想要的行为

编辑:忘了说不需要 setOnClickListener

希望它对任何人都有帮助=)

于 2020-07-16T22:16:05.487 回答