更新答案
经过进一步测试,似乎只有当分隔线的高度严格小于小于为 ListView 设置的dividerHeight 时,分隔线才会显示。例如:
custom_divider.xml
(注意分隔线高度由android:width指定)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:width="1dp"
android:color="$ffff0000" />
</shape>
布局xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:divider="@drawable/custom_divider"
android:dividerHeight="2dp"/>
...会起作用的。但这不会:
custom_divider.xml
(注意分隔线高度由android:width指定)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:width="1dp"
android:color="$ffff0000" />
</shape>
布局xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:divider="@drawable/custom_divider"
android:dividerHeight="1dp"/>
我的猜测是 Google 搞砸了绘制 Listview 分隔线的优化,如果没有足够的空间就不会绘制它们。
原帖
看起来您需要同时设置 ListView 上的 dividerHeight 和可绘制分隔线的笔划 width 才能在 Android 5 上运行。
例子:
custom_divider.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:width="10dp"
android:color="$ffff0000" />
<gradient android:height="1dp" />
</shape>
布局xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:divider="@drawable/custom_divider"
android:dividerHeight="20dp"/>