qwertz
qwertz

Reputation: 6286

Android ListView card behaviour

I just noticed something. When I use a layout inside my ListView I usually use a LinearLayout with then a CardView inside it. The CardView has a margin of 10dp and it all looks fine. However, when I just use the CardView which has the same attributes, it does not give me a margin at all. Is there anything I'm doing wrong?

Layout 1 (Gives me the layout I want):

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardElevation="2sp"
        card_view:cardUseCompatPadding="true">

    </android.support.v7.widget.CardView>

</LinearLayout>

Layout 2 (Without any margin):

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    card_view:cardElevation="2sp"
    card_view:cardUseCompatPadding="true"
    card_view:cardBackgroundColor="@color/white">

</android.support.v7.widget.CardView>

Upvotes: 3

Views: 458

Answers (1)

rubengees
rubengees

Reputation: 1860

I also ran into this problem. I think this happens, because the ListView ignores/doesn't support the margin attribute.

More about this: Why LinearLayout's margin is being ignored if used as ListView row view

A note on the side: I would use FrameLayout around the CardView, it is better performance-wise.

Upvotes: 3

Related Questions