【问题标题】:Card Corner Radius not working in CardView卡片角半径在 CardView 中不起作用
【发布时间】:2021-02-20 08:42:46
【问题描述】:

我正在尝试为我的cardView 实现一个cardCornerRadius,但它不适用于我。

这是我的 XML:

编辑:我没有提到我将 cardView 添加到 recyclerView 列表中,所以我只是调整了 recylcerView 的宽度和高度,现在它可以工作了,但是另一个问题是当我添加 ImageViewcardCornerRadius 无法解决 ImageView

像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="5dp"
    android:orientation="vertical">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/journalListImageView"
            android:layout_width="400dp"
            android:layout_height="250dp"
            android:adjustViewBounds="true"
            android:cropToPadding="true"
            android:scaleType="fitXY"
            android:src="@drawable/quotefancy_work_hard" />

        <TextView
            android:id="@+id/journalTitleTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:padding="5dp"
            android:text="Title"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/journalThoughtsTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:padding="5dp"
            android:text="Toughts..."
            android:textSize="18sp"
            android:textStyle="normal" />

        <TextView
            android:id="@+id/journalTimestampTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:padding="5dp"
            android:text="Date created"
            android:textStyle="italic" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

【问题讨论】:

  • 可以加截图吗?
  • 您的代码是正确的,它对我来说可以正常工作。尝试重新启动 android studio
  • 为问题添加截图
  • 我试过你的代码,没有发现问题。它是由包导入错误引起的还是我可以问你得到的错误信息是什么?

标签: android android-layout android-cardview


【解决方案1】:

你的代码没有问题,我正在尝试修改 app:cardCornerRadius="40dp" 效果。

您可以尝试重新构建项目或重新启动 Android Studio。

【讨论】:

    【解决方案2】:

    cardview 的父布局,需要 padding 来制作卡片角和半径效果。为cardview设置margin也是同样的效果。

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-17
      • 2023-04-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多