BekaKK
BekaKK

Reputation: 2243

CardView custom background

I try to add custom background in my CardView.This is my source

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00"
android:orientation="vertical">


<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="12dp"
    card_view:cardElevation="12dp"
    card_view:cardPreventCornerOverlap="false"
    card_view:contentPadding="0dp"
    android:layout_margin="16dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#cccccc">
        </RelativeLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

And this is a result

As you can see into Cardview i have LinearLayout and RelativieLayout.RelativeLayout's background color is #cccccc,but cornerradius and elevetion not working with custom background color.(please see my picture) How i can solve my problem?

Upvotes: 1

Views: 1711

Answers (1)

Vivek Mishra
Vivek Mishra

Reputation: 5705

Your Linear Layout inside CardView is overlapping your cardview's top corners due to fix height given to linear layout. When giving height to cardview and making linear layout match_parent, and giving contentPadding in cardview makes corners visible.

Here is the code and screenshot.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00"
android:orientation="vertical">


<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    card_view:cardCornerRadius="12dp"
    card_view:cardElevation="12dp"
    card_view:cardPreventCornerOverlap="true"
    card_view:contentPadding="2dp"
    android:layout_margin="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#cccccc">
        </RelativeLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

Screenshot

Image

Upvotes: 2

Related Questions