CardView rounded corners not working in RecyclerView - Android?

My Android device is 4.3 and does not work around the corner of the cardView :

 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/tools" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/CardStart" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:scaleType="centerCrop" app:cardUseCompatPadding="true" card_view:cardBackgroundColor="@color/BlackTrans" card_view:cardCornerRadius="5dp" card_view:cardElevation="0dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/txtTitle" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtDescription" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageButton android:id="@+id/imgbIcon" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@drawable/ic_serch" /> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> 

And I write the following code in my class, but don't work yet:

 if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { holder.CardStart.setCardElevation(0); holder.CardStart.setBackgroundColor(ContextCompat.getColor(context,R.color.BlackTrans)); holder.CardStart.setRadius(5); holder.CardStart.setUseCompatPadding(true); } 
+5
source share
2 answers

It turns out that calling View.setBackgroundColor(int) on the CardView removes the rounded corners.

To change the background color on the map and save the corners, you need to call CardView.setCardBackgroundColor(int) .

This may be the case for some visitors to this post.


With a subclass of CardView I suggest adding the following method to protect your corners from being accidentally deleted:

 /** * Override prevents {@link View#setBackgroundColor(int)} being called, * which removes the rounded corners. */ @Override public void setBackgroundColor(@ColorInt int backgroundColor) { setCardBackgroundColor(backgroundColor); } 

In particular, I worked on implementing a custom view for React Native, and React automatically applied the background color to the view. This redefinition solved this problem; this means that other developers do not need to know the details of the underlying view.

+17
source

Try adding two attributes to your map view.

 card_view:cardPreventCornerOverlap="false" card_view:cardUseCompatPadding="true" 

Here is the documentation for the first attribute (which contains a link to the second) https://developer.android.com/reference/android/support/v7/widget/CardView.html#setPreventCornerOverlap(boolean)

That should do the trick

+2
source

All Articles