Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改卡的背景(Android cardView)_Android_Android Layout_Android Cardview - Fatal编程技术网

更改卡的背景(Android cardView)

更改卡的背景(Android cardView),android,android-layout,android-cardview,Android,Android Layout,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:l

我很难在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>
这应该行得通

<?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"

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

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">

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

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

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

FFCA28

在课堂上使用:


card_name.setCardBackgroundColorgetResources.getColorR.color.ambar400

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

FFCA28

在课堂上使用:


card_name.setCardBackgroundColorgetResources.getColorR.color.ambar400

你能用更多的xml更新吗?你能用更多的xml更新吗?这是最好的答案。这个解决方案对我有效。这是最好的答案。这个解决方案对我有效。