带有圆角的Android CardView显示灰色角落

新手上路,请多包涵

我正在使用布局内的 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 许可协议

阅读 1.1k
2 个回答

就是因为有阴影,需要给cardview留出空间来显示完整的阴影。将 android:layout_margin="5dp" 添加到 CardView ,您将看到“灰色”颜色是剪切阴影。

解决方案 是将 app:cardUseCompatPadding="true" 添加到 CardView,它将提供所需的间距。

原文由 Misha Akopov 发布,翻译遵循 CC BY-SA 3.0 许可协议

试试这个…

只需将 0 值设置为 app:cardElevation

 .....
<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="0dp">
.....

或者 您可以调用 cardView.setCardElevation(0) 以编程方式禁用阴影。

原文由 Silambarasan Poonguti 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题