问题描述
我试图让回收站视图中的图像具有圆角以更好看。我在网上查看,发现我无法将背景设置为 cardview,但我可以将背景设置为线性布局,所以这就是我所做的。在 Android Studio 的设计选项卡中,我可以看到 cardview 有圆角,但是当我运行该应用程序时,角不是圆角。我试着用谷歌搜索这个问题几个小时,但我找不到答案。
这是我在 Android Studio 和模拟器中看到的对比
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dp">
<androidx.cardview.widget.CardView
android:id="@+id/cvPhoto"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:elevation="0dp"
app:cardBackgroundColor="#CABCBC"
app:cardCornerRadius="15dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/llPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/_0443265_10154716579055043_2084238995361491869_o"
android:orientation="horizontal"></LinearLayout>
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/tvName"
android:layout_width="0dp"
android:layout_height="19dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="8dp"
android:text="name"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="@+id/tvSurname"
app:layout_constraintStart_toEndOf="@+id/cvPhoto"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/tvSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="60dp"
android:text="surname"
android:textColor="@color/gold"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/cvPhoto" />
<TextView
android:id="@+id/tvRank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:text="Rank"
android:textColor="@color/gold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.503" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/next_arrow" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方法
我读了一些评论,建议移动你的线性布局以获得你想要的效果,但我建议让你的线性布局而不是这样做:
-
在 cardview 和线性布局之间添加一些填充:
app:cardUseCompatPadding="true" android:padding="16dp"
不要错过 cardUseCompatPadding 标志!
-
像这样输入你的拐角半径和高度:
app:cardElevation="@dimen/cardview_elevation" app:cardCornerRadius="@dimen/cardview_corners_radius"
-
将constraintLayout parent的cardBackground设置为白色和透明色;
app:cardBackgroundColor="@color/white" for cardview android:background="@color/transparent" for parent layout
大功告成。