cardBackgroundColor和cardCornerRadius在AndroidX中不起作用

时间:2019-07-01 12:44:06

标签: android android-cardview androidx

我正在使用AndroidX库来处理CardView角半径和背景颜色。

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
        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"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="@dimen/retail_card_width"
        card_view:cardCornerRadius="@dimen/card_radius"
        card_view:cardBackgroundColor="@color/baseYellow"
        android:layout_height="@dimen/retail_card_height">
    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <ImageView android:layout_width="match_parent" android:layout_height="match_parent"
                   tools:src="@drawable/ic_fruit_1"
                   app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
                   app:layout_constraintStart_toStartOf="parent"
                   android:scaleType="fitEnd"
                   app:layout_constraintTop_toTopOf="parent"/>
        <ImageView
                android:id="@+id/ivRetailBrand"
                android:layout_width="@dimen/brand_icon_size"
                android:layout_height="@dimen/brand_icon_size"
                tools:src="@drawable/esselunga"
                android:layout_marginTop="@dimen/retail_brand_margin"
                android:background="@drawable/round_outline"
                app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="@dimen/retail_brand_margin"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

不幸的是,cardCornerRadiuscardBackgroundColor都无法在我的版面上工作。我不知道我的问题是否取决于AndroidX库。

这是我的布局预览:

cardview issue

11 个答案:

答案 0 :(得分:2)

为此,我可能会迟到,但让我节省以后的时间。我在Android Studio preview上遇到了这个问题,后来才意识到问题是CardView内的内容不是clipped到CardView的边界,所以您看到的角是ImageView的角,当您在模拟器/设备上运行应用程序时,实际上会对其进行裁剪。

我希望这会有所帮助!

答案 1 :(得分:1)

我在清单文件中删除了这些行,然后我的cardviews正常运行了

android:hardwareAccelerated =“ false”

答案 2 :(得分:1)

尝试清理并重建项目。这对我有用。

答案 3 :(得分:0)

尝试将CardView修改为:

<androidx.cardview.widget.CardView
        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="@dimen/retail_card_width"
        app:cardCornerRadius="@dimen/card_radius"
        app:cardBackgroundColor="@color/baseYellow"
        android:layout_height="@dimen/retail_card_height">

答案 4 :(得分:0)

我认为这与CardView设置无关。

检查您的ImageView,这可能是拐角未圆角的原因。

注释掉图像查看代码(整个ConstraintLayout)并检查UI。因为我以前遇到过相同的问题。

答案 5 :(得分:0)

事实证明,问题在于我将cardview androidx库与支持recyclerview库混合在一起。一旦我用以下项目重建项目:

implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'

一切都很好。

答案 6 :(得分:0)

我在应用程序级别gradle中将 compileSdkVersion 从28升级到了29,问题得以解决。

compileSdkVersion 29

答案 7 :(得分:0)

您可以创建一个新的可绘制文件round_corner_colored.xml并将以下代码粘贴到该文件中:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid
    android:color="@color/light_sky_blue"/>

<corners
    android:radius="@dimen/_12sdp"/>

</shape>

现在将此可绘制对象设置为cardview的背景为:

 android:background="@drawable/round_corner_colored"

希望这会有所帮助!

答案 8 :(得分:0)

我有类似的问题。我通过将CardView包装在FrameLayout中解决了它。也许可以帮助某人。

答案 9 :(得分:0)

有些品牌在第一次安装应用时强制 Dark mode 这会影响视图的着色,并且在 CardView 中应用颜色将被忽略。您可以将此应用于您的主题以防止此类事件发生。

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

答案 10 :(得分:0)

对我来说,它在预览中没有正确显示。但它在设备上正确显示。有一些渲染问题。我在android studio中一直遇到这样的问题。

Android Studio 4.2.1 (Mac) 中遇到的问题