首页 > 解决方案 > 较低 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”作为背景。这是文件“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 中运行应用程序时,它看起来像这样(“下一个按钮”): 在 android 7 中预览

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

我不是这种行为是在 Cardview 中。有谁知道我如何解决这个问题?

标签: androidandroid-buttonandroid-cardview

解决方案


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

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


推荐阅读