我正在使用布局内的 CardView 为自定义布局充气。圆角按预期显示,但我在圆角后面也有灰色背景。
代码很简单,使用带有圆角半径和背景颜色的 CardView。我试过设置透明背景但不起作用。但是,如果我设置另一种不透明颜色,它会显示在角落里。
附上代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:background="@android:color/transparent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="6dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<TextView
android:id="@+id/tvProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/ivIcon"
android:layout_toStartOf="@+id/ivIcon"
android:background="@android:color/transparent"
android:padding="@dimen/elementPaddingSmall"
android:text="Initial Discussion"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" />
<ImageView
android:id="@+id/ivIcon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="@color/lightBrown"
android:scaleType="centerInside"
android:src="@drawable/ic_checkmark_circle" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
结果:
原文由 Prasad Pawar 发布,翻译遵循 CC BY-SA 4.0 许可协议
就是因为有阴影,需要给cardview留出空间来显示完整的阴影。将
android:layout_margin="5dp"
添加到 CardView ,您将看到“灰色”颜色是剪切阴影。解决方案 是将
app:cardUseCompatPadding="true"
添加到 CardView,它将提供所需的间距。