98

我有这个代码:

<ListView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/cashItemsList"
     android:cacheColorHint="#00000000"
     android:divider="@drawable/list_divider"></ListView>

哪里@drawable/list_divider是:

<shape
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="line">
 <stroke
   android:width="1dp"
   android:color="#8F8F8F"
   android:dashWidth="1dp"
   android:dashGap="1dp" />
</shape>

但我看不到任何分隔线。

4

12 回答 12

176

伙计们,这就是为什么你应该使用 1px 而不是 1dp 或 1dip:如果你指定 1dp 或 1dip,Android 会缩小它。在 120dpi 的设备上,它变成了 0.75px 的平移,四舍五入为 0。在某些设备上,它转换为 2-3 像素,通常看起来很难看或草率

对于分隔线,如果您想要 1 像素的分隔线,则 1px 是正确的高度,并且是“一切都应该倾斜”规则的例外之一。在所有屏幕上都是 1 个像素。另外,1px 通常在 hdpi 及以上屏幕上看起来更好

“不再是 2012 年”编辑:您可能必须从某个屏幕密度开始切换到 dp/dip

于 2012-08-21T19:21:15.897 回答
56

这是一种解决方法,但对我有用:

创建 res/drawable/divider.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#ffcdcdcd" android:endColor="#ffcdcdcd" android:angle="270.0" />
</shape>

在 listview 项的 styles.xml 中,我添加了以下几行:

    <item name="android:divider">@drawable/divider</item>
    <item name="android:dividerHeight">1px</item>

关键部分是包含这个 1px 设置。当然,drawable 使用渐变(1px),这不是最佳解决方案。我尝试使用中风,但没有让它发挥作用。(您似乎没有使用样式,因此只需为 ListView 添加 android:dividerHeight="1px" 属性。

于 2010-11-07T14:21:17.720 回答
27

添加android:dividerHeight="1px"它会起作用:

<ListView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/cashItemsList"
     android:cacheColorHint="#00000000"
     android:divider="@drawable/list_divider"
     android:dividerHeight="1px">
 </ListView>
于 2011-12-11T11:29:09.130 回答
15

您遇到的问题源于您缺少所需的 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 来设置它。

于 2012-07-08T19:17:44.780 回答
8

从文档:

public void setDivider(Drawable divider) on ListView

/**
 * Sets the drawable that will be drawn between each item in the list. If the drawable does
 * not have an intrinsic height, you should also call {@link #setDividerHeight(int)}
 *
 * @param divider The drawable to use.
 */

如果分隔线setDividerHeight()没有固有高度,则必须调用它才能显示分隔线

于 2011-11-18T19:04:28.923 回答
5

@drawable/list_divide应该是这样的:

<shape
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="line">
 <stroke
   android:height="1dp"
   android:color="#8F8F8F"
   android:dashWidth="1dp"
   android:dashGap="1dp" />
</shape>

在您的版本中,您提供了一个android:width="1dp",只需将其更改为一个android:height="1dp",它应该可以工作!

于 2012-04-16T08:25:22.157 回答
4

文档

文件位置:

res/drawable/filename.xml

文件名用作资源 ID

基本上,您需要放置一个名为list_divider.xmlin的文件,res/drawable/以便您可以访问它R.drawable.list_divider;如果您可以通过这种方式访问​​它,那么您可以android:divider="@drawable/list_divider"在 XML 中使用ListView.

于 2010-10-20T15:14:59.880 回答
2

有些人可能会遇到实线。我通过添加android:layerType="software"到引用可绘制对象的视图来解决这个问题。

于 2014-02-26T04:04:55.007 回答
1

android 文档警告由于舍入错误而消失的事物...也许尝试 dp 而不是 px,也许还可以先尝试 > 1 以查看是否是舍入问题。

http://developer.android.com/guide/practices/screens_support.html#testing

对于“具有 1 像素高度/宽度的图像”部分

于 2011-03-04T03:11:35.093 回答
1

我遇到过同样的问题。然而,在我原来的 Nexus 7 上,将视图设为 1px 似乎不起作用。我注意到屏幕密度为 213,小于 xhdpi 中使用的 240。所以它认为该设备是 mdpi 密度。

我的解决方案是让dimens文件夹有一个dividerHeight参数。我将它设置为 2dpvalues-mdpi文件夹中,但1dpvalues-hdpietc 文件夹中。

于 2014-05-13T20:56:36.393 回答
1

您在分隔符 xml 布局中的分隔符末尾忘记了“r”

你调用布局@drawable/list_divider,但你的分隔器xml被命名为“list_divide”

于 2015-03-05T05:42:20.420 回答
-1

android:dividerHeight="1dp"

<ListView
    android:id="@+id/myphnview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@drawable/dividerheight"
    android:background="#E9EAEC"
    android:clickable="true"
    android:divider="@color/white"
    android:dividerHeight="1dp"
    android:headerDividersEnabled="true" >
</ListView>
于 2016-10-20T10:04:15.103 回答