Android ListView 分隔符

发布于 2024-09-28 07:24:27 字数 608 浏览 7 评论 0原文

我有这样的代码:

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

但我看不到任何分隔线。

I have this code:

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

where @drawable/list_divider is:

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

but I can't see any divider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

So要识趣 2024-10-05 07:24:27

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

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

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

Folks, here's why you should use 1px instead of 1dp or 1dip: if you specify 1dp or 1dip, Android will scale that down. On a 120dpi device, that becomes something like 0.75px translated, which rounds to 0. On some devices, that translates to 2-3 pixels, and it usually looks ugly or sloppy

For dividers, 1px is the correct height if you want a 1 pixel divider and is one of the exceptions for the "everything should be dip" rule. It'll be 1 pixel on all screens. Plus, 1px usually looks better on hdpi and above screens

"It's not 2012 anymore" edit: you may have to switch over to dp/dip starting at a certain screen density

满身野味 2024-10-05 07:24:27

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

创建了 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" 属性即可。

This is a workaround, but works for me:

Created res/drawable/divider.xml as follows:

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

And in styles.xml for listview item, I added the following lines:

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

Crucial part was to include this 1px setting. Of course, drawable uses gradient (with 1px) and that's not the optimal solution. I tried using stroke but didn't get it to work. (You don't seem to use styles, so just add android:dividerHeight="1px" attribute for the ListView.

两相知 2024-10-05 07:24: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>

Add android:dividerHeight="1px" and it will work:

<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>
沧笙踏歌 2024-10-05 07:24:27

您遇到的问题源于您缺少 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>

我假设您知道如何使用这些片段,如果不知道的话请告诉我。基本上你的问题的答案是你不能在可绘制对象中设置分隔线厚度,你必须在那里保留未定义的宽度并使用 android:dividerHeight 来设置它。

The problem you're having stems from the fact that you're missing android:dividerHeight, which you need, and the fact that you're trying to specify a line weight in your drawable, which you can't do with dividers for some odd reason. Essentially to get your example to work you could do something like the following:

Create your drawable as either a rectangle or a line, either works you just can't try to set any dimensions on it, so either:

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

OR:

<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">
     <solid android:color="#8F8F8F"/>
</shape>

Then create a custom style (just a preference but I like to be able to reuse stuff)

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

Finally declare your list view using the custom style:

<ListView
     style="@style/dividedListStyle"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/cashItemsList">
</ListView>

I'm assuming you know how to use these snippets, if not let me know. Basically the answer to your question is that you can't set the divider thickness in the drawable, you have to leave the width undefined there and use android:dividerHeight to set it instead.

旧时浪漫 2024-10-05 07:24:27

从文档中:

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

From the doc:

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.
 */

Looks like setDividerHeight() must be called in order for the divider to show up if it has no intrinsic height

佞臣 2024-10-05 07:24:27

您的 @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” 它应该可以工作!

Your @drawable/list_divide should look like this:

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

In your version you provide an android:width="1dp", simply change it to an android:height="1dp" and it should work!

就像说晚安 2024-10-05 07:24:27

来自 doc

文件位置:

res/drawable/filename.xml

文件名用作资源 ID

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

From the doc:

file location:

res/drawable/filename.xml

The filename is used as the resource ID.

basically, you'll need to put a file named list_divider.xml in res/drawable/ so you can access it as R.drawable.list_divider; if you can access it that way, then you can use android:divider="@drawable/list_divider" in the XML for ListView.

天气好吗我好吗 2024-10-05 07:24:27

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

Some people might be experiencing a solid line. I got around this by adding android:layerType="software" to the view referencing the drawable.

绾颜 2024-10-05 07:24:27

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

请参阅http://developer.android.com/guide/practices/screens_support.html#

测试“高度/宽度为 1 像素的图像”部分的

The android docs warn about things dissappearing due to round-off error... Perhaps try dp instead of px, and perhaps also try > 1 first to see if it is the round-off problem.

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

for the section "Images with 1 pixel height/width"

过潦 2024-10-05 07:24:27

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

我的解决方案是让 dimens 文件夹具有 dividerHeight 参数。我在 values-mdpi 文件夹中将其设置为 2dp,但在 values-hdpi 等文件夹中将其设置为 1dp

I had the same issue. However making the view 1px didn't seem to work on my original Nexus 7.
I noticed that the screen density was 213 which is less than the 240 used in xhdpi. So it was thinking the device was an mdpi density.

My solution was to make it so the dimens folder had a dividerHeight parameter. I set it to 2dp in the values-mdpi folder but 1dp in the values-hdpi etc folders.

绝不服输 2024-10-05 07:24:27

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

您将布局命名为 @drawable/list_divider 但您的分隔符 xml 名为“list_divide”

you forgot an "r" at the end of divider in your divider xml layout

you call the layout @drawable/list_divider but your divider xml is named "list_divide"

神经暖 2024-10-05 07:24:27

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

Set 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文