android.support.v7.cardview :cardBackgroundColor doesn't work

标签 android android-cardview

我在资源文件中有以下 XML 代码,它表示 RecyclerView 中的一个项目:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android.support.v7.cardview="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android.support.v7.cardview:cardBackgroundColor="#9e9e9e"
    android.support.v7.cardview:cardElevation="7dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/type"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_gravity="end" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/field_name"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:padding="2dp"
                    android:textColor="#000000"
                    android:textSize="20sp"
                    android:typeface="monospace" />
                <LinearLayout
                    android:layout_width="15dp"
                    android:layout_height="match_parent" />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/main_recyclerview_item_actions"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="end"
                android:visibility="gone">
                <Button
                    android:id="@+id/delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/delete"
                    android:textColor="@color/colorAccent"
                    style="@style/Widget.AppCompat.Button.Borderless"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

问题是 CardView 永远不会得到颜色:9e9e9e,如下图所示: 颜色是白色不是9e9e9e

enter image description here

代码哪里出了问题?我该如何解决这个问题?

最佳答案

只需删除 cardBackgroundColor 属性并将 android:background="#9e9e9e" 设置为布局中的第一个 LinearLayout。我的意思是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android.support.v7.cardview="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android.support.v7.cardview:cardElevation="7dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#9e9e9e"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/type"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_gravity="end" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/field_name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="2dp"
                android:textColor="#000000"
                android:textSize="20sp"
                android:typeface="monospace" />

            <LinearLayout
                android:layout_width="15dp"
                android:layout_height="match_parent" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/main_recyclerview_item_actions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:visibility="gone">

            <Button
                android:id="@+id/delete"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="DELETE"
                android:textColor="@color/colorAccent" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

关于android.support.v7.cardview :cardBackgroundColor doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36216495/

相关文章:

android - 单击 CardView 时的高程动画

android - CardView 示例不工作

android - 升高的 RecyclerView 项目逐渐变形(海拔变化)

java - 如何在传递上下文时调用另一个类的方法?

android - ffmpeg压缩后反转视频分辨率

java - 为什么 "can' 找不到 android.support.design.widget.TextInputLayout”?

android - Android Wear 卡片中的文字大小和颜色是多少

android - 如何调用其他项目的 Activity ?

iphone 开发限制

android - gridview 中的最后一个 cardview 未显示在 ScrollView 布局中