Cardview corner background not transparent

强颜欢笑 提交于 2019-11-30 07:06:55
Just89

Use the following code, on a place where the context is available, in your custom implementation:

setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));

EDIT:

Use the following code for android versions lower than Lollipop to avoid the mentioned crash.

  if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
     getBackground().setAlpha(0);
  } else {
     setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent);
  }

After a few years I came to the conclusion that I was making a fundamental mistake when creating compound components.

When making a compound component that extends android.support.v7.widget.CardView the layout xml that's inflated shouldn't also contain a CardView. Basically you'll just be adding a CardView to another CardView which will result in the white corners behind my custom CardView.

The accepted answer will solve this problem. However it's not the perfect solution.

There are 2 ways to fix the real problem:

  1. The compound view extends android.support.v7.widget.CardView and the layout xml doesn't contain android.support.v7.widget.CardView but a LinearLayout instead as the root. Styling will have to handled in the class.
  2. The compound view extends LinearLayout and the layout xml contains android.support.v7.widget.CardView as the root.

I chose the 1st solution to fix this problem. I hope this helps people that made the same mistake.

It looks like you are creating a custom CardView form your layout file because of this

<some.private.namespace.CardView
        android:id="@+id/custom_card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/default_margin" />

Your CardView might be extending something(say LinearLayout) and you may be creating another child view inside that parent View. So just try to set immediate parent of your card layout to transparent using

setBackground();

May be this helps.

You could try nesting your rounded corner view inside another view/layout. The outer view here could have a transparent background, so that even when the rounded corners of the inner leave space on the corners, that will not be seen since the outer view has a transparent background color. Something like this :

<RelativeLayout>
android:background = "@color/transparent"
< some.private.namespace.CardView
android:margin="8dp"
.....
/>
</RelativeLayout>

add new style into styles.xml file similar this :

<style name="CardViewRadius">
    <item name="cardBackgroundColor">@color/colorGray</item>
    <item name="cardCornerRadius">18dp</item>
</style>

and use :

<android.support.v7.widget.CardView
        style="@style/CardViewRadius"
        android:layout_marginTop="15dip"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"/>

and result :

I was having similar issue with a darker area/background where the curved corners are as shown in this screenshot.

.

I solved by setting the cardElevation to 0dp. This may not work for you if you need the shadow.

app:cardElevation="0dp"

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