较低的android版本上的CardView设计问题

问题描述

当我使用“ CardView”创建带有圆角的按钮时,遇到一个奇怪的问题。让我向您解释我面临的设计问题。这是用于制作圆角按钮的代码

<androidx.cardview.widget.CardView
    android:id="@+id/tv_email_next"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv_error_message"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginTop="20dp"
    app:cardBackgroundColor="#f15b5d"
    app:cardCornerRadius="34dp"
    app:cardElevation="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/submit_btn_bg">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/next"
            android:textSize="16sp"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:layout_centerVertical="true"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

我在RelativeLayout中将“ submit_btn_bg”用作backgroudn。这是文件“ submit_btn_bg”的代码

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#f15b5d" />
<corners android:radius="24dp" />
</shape>

现在,当我在android 7.0中运行应用程序时,它看起来像这样(“下一个按钮”):

Preview in android 7

但是当我在Android 10中运行相同的代码时,它看起来像这样:

Preview in android 10

我不是这种现象在Cardview中。有人知道我该怎么解决吗?

解决方法

这是因为您应该输入半径的cardViews高度dp的一半。如果您的Cardview高度为50dp,请在Javascript中将25 dp作为半径。

我还建议您使用按钮,因为它比使用Cardview作为按钮要好得多。

,

这是上面Button的更好表示,输出与您的非常相似,并且代码较少。

<com.google.android.material.button.MaterialButton
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginEnd="32dp"
    android:backgroundTint="#F44336"
    android:padding="8dp"
    android:text="Next"
    android:textAllCaps="false"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:theme="@style/Theme.MaterialComponents"
    app:cornerRadius="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

输出: enter image description here