圆形左上角/右上角 CardView Android 不工作

问题描述

我有一个材质卡片视图作为根项目。我想在它的左上角和右上角添加圆角。我在它的样式中添加了这些属性,但它稍微改变了一点,不能正常工作。

ItemForRV:

<?xml version="1.0" encoding="utf-8"?>

<com.google.android.material.card.MaterialCardView
    android:layout_width="@dimen/_150sdp"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/CardViewRoundedtop"
    xmlns:android="http://schemas.android.com/apk/res/android">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
 >


            <com.google.android.material.textview.MaterialTextView
                android:background="@color/primaryColor"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toTopOf="@id/imgFastfood"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                tools:text="FastFood"
                android:id="@+id/TVNameCategory"
                />


            <ImageView
                app:layout_constraintTop_toBottomOf="@id/TVNameCategory"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_110sdp"
                tools:src="@drawable/fastfood"
                android:id="@+id/imgFastfood"
                />

        </androidx.constraintlayout.widget.ConstraintLayout>


    </com.google.android.material.card.MaterialCardView>

风格:

<style name="CardViewRoundedtop" parent="Widget.MaterialComponents.CardView">

<item name="android:topLefTradius">16dp</item>
    <item name="android:topRighTradius">16dp</item>
    <item name="android:bottomLefTradius">0dp</item>
    <item  name="android:bottomrighTradius">0dp</item>

</style>

P.S:我读到一个错误,我必须将左下/右下半径更改为 1dp。它根本没有帮助。

现在:

enter image description here

我期待的东西:

enter image description here

为了在这里显示,我使用了 app:cardCornerRadius="16dp" 并且设置了所有四个角。

编辑: 我使用了“cornerSizetopRight”。它似乎以不同的方式工作。

enter image description here

解决方法

好吧,我为您找到了解决方案。只需将背景颜色从 TextView 更改为 CardView。所以从 MaterialTextView

中删除下一行
android:background="@color/colorPrimary"

并将这些行添加到您的 MaterialCardView

app:cardBackgroundColor="@color/colorPrimary"
app:cardPreventCornerOverlap="false"

也在您的 ImageView 中添加:

android:scaleType="centerCrop"

之后你会得到这个:

enter image description here

这是我的完整XML code

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    style="@style/MyCardView"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    app:cardBackgroundColor="#EDC911"
    app:cardPreventCornerOverlap="false"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.textview.MaterialTextView
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/imgFastfood"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            tools:text="FastFood"
            android:id="@+id/TVNameCategory"/>

        <ImageView
            app:layout_constraintTop_toBottomOf="@id/TVNameCategory"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_width="match_parent"
            android:scaleType="centerCrop"
            android:layout_height="110dp"
            tools:src="@drawable/Screenshot_1"
            android:id="@+id/imgFastfood"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

编辑:

如果您的图片是透明的并且背景是黄色的,只需将此行添加到您的 ImageView

android:background="@android:color/white

这是在style.xml

  <style name="MyCardView" parent="@style/Widget.MaterialComponents.CardView">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MaterialCardView.Cut</item>
  </style>


  <style name="ShapeAppearanceOverlay.MaterialCardView.Cut" parent="">
    <item name="cornerFamilyTopLeft">rounded</item>
    <item name="cornerFamilyTopRight">rounded</item>
    <item name="cornerSizeTopRight">8dp</item>
    <item name="cornerSizeTopLeft">8dp</item>
    <item name="cornerSizeBottomRight">0dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>
  </style>