【问题标题】:Android - only the last item shows in layer-listAndroid - 只有最后一项显示在图层列表中
【发布时间】:2013-01-22 10:28:17
【问题描述】:

我正在尝试为ListView 设置divider 的样式。我想要的只是两条简单的水平线。

list.xml

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/ListView" >
</ListView>

styles.xml

<style name="ListView" parent="android:style/Widget.ListView">
    <item name="android:divider">@drawable/divider</item>

    <!-- 5dp: just to make sure there's enough space -->
    <item name="android:dividerHeight">5dp</item>
</style>

divider.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#00ffff" />
            <size android:height="1dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ff0000" />
            <size android:height="1dp" />
        </shape>
    </item>
</layer-list>

结果是一条带有height of 5dp(应该是2,不是吗?)和color of red(第二个项目的颜色)的水平线。第一个颜色为#00ffff 的项目根本不显示。

有人可以推荐2条简单水平线的代码吗?

【问题讨论】:

    标签: android android-listview shape divider


    【解决方案1】:

    我最好回答我自己的问题...

    似乎styles.xml - style - itemlayer-listlast item 的颜色作为整个分隔线的背景。这个last item 位于其他层的顶部,这就是为什么层列表中的其他项目不显示的原因。

    密钥是padding

    styles.xml

    <style name="ListView" parent="android:style/Widget.ListView">
        <item name="android:divider">@drawable/divider</item>
        <item name="android:dividerHeight">10dp</item>
    </style>
    

    divider.xml

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#0000ff" />
                <padding android:top="5dp"/>
            </shape>
        </item>
    
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#ff0000" />
            </shape>
        </item>
    
    </layer-list>
    

    所以在我的情况下,我只需将 10dp/5dp 更改为 2dp/1dp 即可获得 2 条细水平线。

    【讨论】:

    • 这是一个很好的答案!仅使用最后一项背景。在我的情况下,我的最后一个图层列表项只是一个笔划,所以没有形状也没有颜色,这导致了一个全黑的背景! (在 android 4.4 中它可以工作,但在 2.2 中没有,未测试其他 API)。
    猜你喜欢
    • 2021-12-15
    • 2019-01-26
    • 2019-07-10
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多