即使我通过android:backround设置了背景绘制,CardView(android.support.v7.cardview)仍然保持白色。文档让我感觉它应该起作用。不知道我在这里做错了什么。
我知道这是一个老问题,但我有一个简单的解决方案 - 只需将你的CardView的第一个子项设为ImageView,并指定scale type为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);
但是这个并没有产生预期的结果
制作 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>
<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_view_bg 是一个自定义的 XML 资源文件。
如果您在卡片视图中有一些布局,而这些布局与卡片视图的边距重叠,则您可能需要为这些布局创建一个单独的自定义背景资源文件,就像我为卡片视图背景本身使用的那个一样。
您可以在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"/>
试试这个
<?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背景设置为相应的可绘制对象。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.setBackgroundResource(R.drawable.background)
<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的形状进行配置。缺点是它对性能不利。
sCard.setBackgroundResource(R.drawable.scard_background);