【问题标题】:How do I create a ListView with dashed / dotted line dividers in Android?如何在 Android 中使用虚线/虚线分隔线创建 ListView?
【发布时间】:2011-05-06 10:49:33
【问题描述】:

我设法通过在 /app/res/drawable/ em> 文件夹:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:color="#534b4b"
        android:width="1dp"
        android:dashGap="2dp"
        android:dashWidth="1dp"
    />
    <size
        android:height="1dp"
    />
</shape>

现在我对如何将此形状应用到 ListView 感到困惑。我尝试了以下方法,但没有显示分隔符:

<ListView android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:cacheColorHint="#00000000"
    android:divider="@drawable/dash"
    android:dividerHeight="1dp"
/>

什么?

【问题讨论】:

    标签: android xml listview android-layout android-listview


    【解决方案1】:

    这是我的,它可以工作:

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line">
        <stroke
            android:color="#FF404040"
            android:width="1dp"
            android:dashGap="3dp"
            android:dashWidth="1dp"
        />
        <size
            android:height="3dp"
        />
    </shape>
    

    【讨论】:

    • 您可能还需要将 android:layerType="software" 添加到引用该形状的节点。至少在某些设备上,虚线会显示为实线。
    • 作为 layerType 解决实线问题的设备的示例是 Nexus 5 和 4.4.3。
    • 这个版本似乎可以工作,因为 height="3dp"。这在我看来就像 GradientDrawable 中的一个错误:如果高度 - 2 * 笔划宽度
    • 另外,如前所述,您必须为较新的 android 版本使用软件层,因为硬件加速不支持虚线。见code.google.com/p/android/issues/detail?id=29944
    【解决方案2】:

    您还需要android:dividerHeight。形状是可变大小的,现在你有一个零高度分隔线。

    【讨论】:

    • Shoot,我忘了在上面的示例中添加它,但是在测试时,包含了一个 dividerHeight。感谢您指出这一点!
    【解决方案3】:
    1. 定义 layerType 以显示虚线分隔线,不要忘记提供分隔线高度。

    <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:divider="@drawable/dash" android:dividerHeight="1dp" android:layerType="software"/>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多