cardBackgroundColor and cardCornerRadius not working in AndroidX

守給你的承諾、 提交于 2020-01-24 10:33:04

问题


I'm struggling with CardView corner radius and background color with AndroidX libraries.

I've defined my layout as below:

<?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>

Unfortunately, neither cardCornerRadius nor cardBackgroundColor seem working on my layout. I can't understand if my issue depends on AndroidX libraries or not.

Here there's my layout preview:


回答1:


It turned out that the problem was I was mixing cardview androidx library with support recyclerview library. Once I rebuilt the project with:

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

everything turned out fine.




回答2:


Try modifying the CardView as:

<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">



回答3:


I don't think it's related to the CardView setup.

Check Your ImageView they can be the reason for corner's not being rounded.

Comment Out Image View Code(whole ConstraintLayout) and check the UI.As i had the Same Issue in Past.




回答4:


I might be late for this but let me save someone's time in the future. I had this problem on my Android Studio preview and came to realise that the problem is that the content inside the CardView is not clipped to the bounds of the CardView, so the corners you see are corners of the ImageView, which is actually clipped when you run the app on emulator/device.

I hope this helps!




回答5:


I upgraded compileSdkVersion from 28 to 29 in app level gradle and the problem solved.

compileSdkVersion 29


来源:https://stackoverflow.com/questions/56835962/cardbackgroundcolor-and-cardcornerradius-not-working-in-androidx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!