Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<layer-list> background disables android:padding setting

I have a ListView with custom items layout and custom background. The problem is android:padding value seems to be ignored after I switched background drawable from <shape> to <layer-list>.

layout/list_item.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_list_item_bill"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="12dp">

    . . .
</LinearLayout>

drawable/list_item_background.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
        <solid android:color="@color/white" />
        <corners android:radius="@dimen/corner_radius" />
        <stroke android:width="1dp" android:color="@color/blue" />
</shape>

The above works as expected. Padding disappears when I change drawable/list_item_background.xml to

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape>
            <solid android:color="@color/white"/>
        </shape>
    </item>

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
            <corners android:radius="@dimen/corner_radius" />
            <stroke android:width="1dp" android:color="@color/blue" />
        </shape>
    </item>

</layer-list>

What am I missing?

like image 253
Alexander Zhak Avatar asked Sep 29 '22 17:09

Alexander Zhak


1 Answers

Wrapping <layer-list> in a <selector> has solved the issue:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item>
                <shape>
                    <solid android:color="@color/white"/>
                </shape>
            </item>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/white" />
                    <corners android:radius="@dimen/corner_radius" />
                    <stroke android:width="1dp" android:color="@color/blue" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>
like image 88
Alexander Zhak Avatar answered Oct 05 '22 07:10

Alexander Zhak