您遇到的问题源于您缺少所需的 android:dividerHeight,以及您尝试在可绘制对象中指定线宽的事实,而对于某些人来说,您无法使用分隔线奇怪的原因。基本上为了让您的示例正常工作,您可以执行以下操作:
将您的可绘制对象创建为矩形或线条,两者都可以,您不能尝试在其上设置任何尺寸,因此:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
<stroke android:color="#8F8F8F" android:dashWidth="1dp" android:dashGap="1dp" />
</shape>
或者:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#8F8F8F"/>
</shape>
然后创建一个自定义样式(只是一个偏好,但我喜欢能够重用东西)
<style name="dividedListStyle" parent="@android:style/Widget.ListView">
<item name="android:cacheColorHint">@android:color/transparent</item>
<item name="android:divider">@drawable/list_divider</item>
<item name="android:dividerHeight">1dp</item>
</style>
最后使用自定义样式声明您的列表视图:
<ListView
style="@style/dividedListStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cashItemsList">
</ListView>
我假设你知道如何使用这些片段,如果不告诉我。基本上你的问题的答案是你不能在drawable中设置分隔线的厚度,你必须在那里留下未定义的宽度并使用 android:dividerHeight 来设置它。