繁体   English   中英

Android ListView 分隔线

[英]Android ListView Divider

我有这个代码:

<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>

但我看不到任何分隔线。

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

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

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

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

创建 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>

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

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

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

添加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>

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

从文档:

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()才能让分隔线在没有固有高度的情况下显示

你的@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"就可以了!

文档

文件位置:

res/drawable/filename.xml

文件名用作资源 ID

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

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

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

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

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

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

android 文档警告有关由于舍入错误而消失的事情......也许尝试使用 dp 而不是 px,也可能首先尝试 > 1 以查看它是否是舍入问题。

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

对于“具有 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM