我正在布局中使用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>
结果:
这是因为影子,你需要给cardview提供空间以显示完整的阴影。将android:layout_margin="5dp"
添加到CardView,您将看到“灰色”颜色是剪切阴影。
解决方案是将app:cardUseCompatPadding="true"
添加到CardView,它将提供所需的间距。
试试这个...
只需将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)以编程方式禁用阴影。
删除包裹RelativeLayout
的父级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="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>
如果有人在边缘的颜色比您设置的颜色更深时遇到问题,则可能会在#AARRGGBB
格式中设置颜色时发生。要解决此问题,只需将颜色设置为正常的#RRGGBB
格式即可。