5

我有一个ListView自定义项目布局和自定义背景。问题是在我将背景可绘制对象从 切换到android:padding后,值似乎被忽略了。<shape><layer-list>

布局/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>

可绘制/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>

以上按预期工作。drawable/list_item_background.xml当我更改为时,填充消失

<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>

我错过了什么?

4

1 回答 1

4

包装<layer-list>a<selector>解决了这个问题:

<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>
于 2014-10-04T23:47:47.053 回答