Android 可绘制为CardView的背景

Android 可绘制为CardView的背景,android,android-layout,Android,Android Layout,CardView(android.support.v7.CardView)保持白色,即使我通过android:backround设置了一个可绘制的回溯-文档让我感觉它应该可以工作。不知道我做错了什么。我知道这是一个老问题,但我有一个简单的解决方案-只需将CardView的第一个子项设置为ImageView,并将比例类型指定为fitXY。通过将cardElevation和cardMaxElevation设置为0dp,可以消除额外的CardView填充: <?xml version="1.0"

CardView(android.support.v7.CardView)保持白色,即使我通过android:backround设置了一个可绘制的回溯-文档让我感觉它应该可以工作。不知道我做错了什么。

我知道这是一个老问题,但我有一个简单的解决方案-只需将CardView的第一个子项设置为ImageView,并将比例类型指定为fitXY。通过将cardElevation和cardMaxElevation设置为
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>

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

cvSellerShopp.setBackgroundResource(R.drawable.ic_action_add_image);
颜色使用:

 cvSellerShopp.setCardBackgroundColor(R.color.colorPrimary);

但是这一个不会产生预期的结果

在本例中,Make Cardview将为eg relative layout托管一个视图组,然后简单地将任何背景设置为relative layout

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

试试这个

<?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,请放置相对的背景,你可以在CardView中使用线性布局或相对布局,并设置可绘制背景,如下所示:

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


我通过在cardview中添加一个linearlayout,然后在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>

我使用的命令是:

cardView.setBackgroundResource(R.drawable.card_view_bg);
其中,card\u view\u bg是一个自定义XML资源文件


如果在“卡片视图”中有一些布局,并且这些布局与“卡片视图”的边距重叠,则可能需要一个单独的自定义背景资源文件来处理这些布局,就像我用于“卡片视图”背景本身一样。

您只需在xml文件中使用下一行即可

app:cardBackgroundColor="@drawable/your_custom_theme_here"

如果要更改cardView的背景色,只需在cardView XML中添加此代码

app:cardBackgroundColor="@color/whatever_color_you"

您可以如下设置前景,以便卡视图设置自定义背景

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>

您有两种可能的解决方案

  • 以编程方式设置背景:
  • 向cardview添加一些将填充cardview边界并在其上设置背景的子项:
  • 
    
    通过这种方式,您还可以为图像提供一些形状,您可以通过cardview的形状进行配置。缺点是这对性能不利。

    请尝试以下代码:

    cardView.setForeground(getResources().getDrawable(R.drawable.your_drawable));
    

    看起来这是不可能的。看看这里有没有一种方法可以在不放弃立面阴影的情况下做到这一点?为什么这个答案不受欢迎?这对我很有效。因为问题是关于可绘制而不是颜色。
    <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.setForeground(getResources().getDrawable(R.drawable.your_drawable));