卡片视图的背景可使用可绘制对象作为背景。

65
即使我通过android:backround设置了背景绘制,CardView(android.support.v7.cardview)仍然保持白色。文档让我感觉它应该起作用。不知道我在这里做错了什么。

1
看起来似乎不可能。在这里看看 https://code.google.com/p/android/issues/detail?id=77843 - Blackbelt
13个回答

73

我知道这是一个老问题,但我有一个简单的解决方案 - 只需将你的CardView的第一个子项设为ImageView,并指定scale typefitXY。你可以通过将cardElevationcardMaxElevation设置为0dp来去掉多余的CardView填充:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:cardCornerRadius="4dp"
    app:cardElevation="0dp"
    app:cardMaxElevation="0dp">

    <ImageView
        android:src="@drawable/your_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>

    <... your layout

    .../>


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

9
有没有一种方法可以在不放弃高程阴影的情况下完成这个操作? - amitavk
谢谢兄弟,你帮我省了好多事! - Nghien Nghien

19

对于可绘制或颜色,只需使用:

cvSellerShopp.setBackgroundResource(R.drawable.ic_action_add_image);

用于颜色:

 cvSellerShopp.setCardBackgroundColor(R.color.colorPrimary);

但是这个并没有产生预期的结果


14

制作 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/list_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="4dp"
        card_view:cardUseCompatPadding="true">

        <RelativeLayout
            android:id="@+id/list_container_bg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical">

           <!--Add cardview contents-->

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

14
我通过在cardview中添加线性布局,然后将cardPreventCornerOverlap设置为false,使其正常工作。
   <android.support.v7.widget.CardView
    android:id="@+id/result_cv"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_margin="4dp"
    app:cardCornerRadius="16dp"
    app:cardElevation="8dp"
    app:cardPreventCornerOverlap="false"
    >

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/gradient"
        android:gravity="center"
        >

       <!-- your views -->

    </LinearLayout>

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

7
我使用的命令是:
cardView.setBackgroundResource(R.drawable.card_view_bg);

card_view_bg 是一个自定义的 XML 资源文件。

如果您在卡片视图中有一些布局,而这些布局与卡片视图的边距重叠,则您可能需要为这些布局创建一个单独的自定义背景资源文件,就像我为卡片视图背景本身使用的那个一样。


4

您可以在CardView中使用LinearLayout或RelativeLayout,并像下面一样设置可绘制的背景:

<android.support.v7.widget.CardView
    android:id="@+id/card6"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dp"
    android:layout_marginTop="10dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/card4"
    app:cardCornerRadius="10dp"
    app:cardElevation="4dp"
    app:cardMaxElevation="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/card_bg"
        android:padding="10dp">

        <ImageView
            android:id="@+id/card6Image"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center_horizontal"
            android:src="@mipmap/ic_launcher_round" />

        <TextView
            android:id="@+id/card6Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="5dp"
            android:text="Daily Check In"
            android:textColor="#ffffff"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/card6Description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="5dp"
            android:text="Just Check in Daily and Earn Credits"
            android:textColor="#ffffff"
            android:textSize="10sp" />
    </LinearLayout>

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

以下是我的可绘制资源文件:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" >
<gradient
    android:angle="90"
    android:centerColor="#555994"
    android:endColor="#b5b6d2"
    android:startColor="#555994"
    android:type="linear" />

<corners
    android:radius="0dp"/>


4

试试这个

<?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/list_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:id="@+id/list_container_bg"
        android:layout_width="match_parent"
        android:background="@drawable/yourbackground"
        android:layout_height="match_parent">

       <!--Add cardview contents-->

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

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

在这里,您可以设置任何颜色或通过给予透明颜色使其透明。如果您想要一些像图像或SVG之类的可绘制对象,请将RelativeLayout背景设置为相应的可绘制对象。


4
你可以按照以下方式设置前景色,为卡片视图设置自定义背景。
android:foreground="@drawable/bg"

这里是透明背景 bg.xml 文件。

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <stroke
        android:width="1dp"
        android:color="#d9d9d9" />
    <corners android:radius="4dp" />
    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />
</shape>

3

你有两个可能的解决方案。

  1. 通过编程设置背景:
cardView.setBackgroundResource(R.drawable.background)
  1. 向 CardView 中添加一些子视图,以填满 CardView 的边界,并在其上设置背景:
<android.support.v7.widget.CardView
...>
     <ConstraintLayout
        android:background="@drawable/background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.v7.widget.CardView>

通过这种方式,您也可以使图像具有某些形状,您可以通过cardview的形状进行配置。缺点是它对性能不利。


0
sCard.setBackgroundResource(R.drawable.scard_background);

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