【问题标题】:CardCornerRadius is not clipping linear layout within viewCardCornerRadius 没有在视图中剪裁线性布局
【发布时间】:2021-02-21 06:07:12
【问题描述】:

我正在尝试让我的回收站视图中的图像具有圆角以看起来更好。我在网上查看,发现我无法将背景设置为卡片视图,但我可以将背景设置为线性布局,这就是我所做的。在 Android Studio 的设计选项卡中,我可以看到卡片视图有圆角,但是当我运行应用程序时,角不是圆角的。我尝试用谷歌搜索这个问题几个小时,但找不到答案。

这是我在 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>

【问题讨论】:

  • 也许您的线性布局背景与您的卡片视图的角半径重叠。尝试删除一次线性布局,你就会知道你是否真的有一个角半径来卡片视图。
  • 我将线性布局设置为不可见,当我运行我的应用程序时,我看到卡片视图具有我想要的线性布局的圆角。
  • 我想您可以简单地使用 ImageView 而不是线性布局,它应该可以满足您的要求。即使这不起作用,您也需要为线性布局提供角半径

标签: java android android-linearlayout android-cardview rounded-corners


【解决方案1】:

我阅读了一些建议移动您的线性布局以获得您想要的效果的 cmets,但我建议让您的线性布局改为这样做:

  1. 在您的卡片视图和线性布局之间添加一些填充:

    app:cardUseCompatPadding="true"
    android:padding="16dp"
    

    不要错过 cardUseCompatPadding 标志!

  2. 把你的拐角半径和高度像这样:

    app:cardElevation="@dimen/cardview_elevation"
    app:cardCornerRadius="@dimen/cardview_corners_radius"
    
  3. 将约束布局父级的cardBackground设置为白色和透明色;

    app:cardBackgroundColor="@color/white" for cardview
    android:background="@color/transparent" for parent layout
    

你已经完成了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多