更改卡片的背景(Android cardView)

changing the background of a card (Android cardView)

我很难在 Android (cardView) 中更改卡片的背景颜色。

我在 XML:

<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:layout_margin="5dp"
        android:layout_gravity="bottom"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp"
        card_view:cardBackgroundColor="#2600e622">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

但是没有用。我一直默认为白色背景颜色。

我不知道从这里到哪里去。 有什么想法吗?

此外 - 我想将卡片放置在屏幕中央,卡片最终位于屏幕中央但位于屏幕顶部。 我也可以了解如何将卡片放置在屏幕中央吗?

谢谢。

这应该有效。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_margin="5dp"
    android:layout_gravity="center"
    card_view:cardCornerRadius="2dp"
    card_view:contentPadding="10dp"
    card_view:cardBackgroundColor="#00490b">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        // add more views here

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

我使用这段代码以编程方式设置:

card.setCardBackgroundColor(Color.WHITE);

或者在 XML 中,您可以使用此代码:

card_view:cardBackgroundColor="@android:color/white"

如果他是你cardview的根布局,设置颜色为你的相对布局背景:

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:colorBackground="#2600e622">

如果您的colors.xml中有这种颜色,例如:

<color name="ambar400">#FFCA28</color>

在您的 class 中使用:

card_name.setCardBackgroundColor(getResources().getColor(R.color.ambar400));