【问题标题】:CardView design issue on lower android versions较低 android 版本上的 CardView 设计问题
【发布时间】:2020-09-11 05:38:54
【问题描述】:

当我使用“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 10 中运行相同的代码时,它看起来像这样:

我不是这种行为是在 Cardview 中。有人知道我是怎么解决的吗?

【问题讨论】:

  • 没有理由将 CardView 与 TextView 一起使用只是为了实现圆角按钮。只需使用 MaterialButton。

标签: android android-button android-cardview


【解决方案1】:

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

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

【讨论】:

  • 别忘了投票或检查这个答案兄弟。这样你的这个问题就不会堆积在饲料中。泰。
  • 我的朋友,您似乎是对的,我已经测试并确认了。添加了一个赞成票!
【解决方案2】:

这是上面 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" />

输出:

【讨论】:

  • 使用app:backgroundTint 而不是android:backgroundTint
猜你喜欢
  • 2014-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 2014-11-25
相关资源
最近更新 更多