Android ListView 分隔符
我有这样的代码:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
各位,这就是为什么你应该使用 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
这是一种解决方法,但对我有用:
创建了 res/drawable/divider.xml,如下所示:
在 listview 项目的 styles.xml 中,我添加了以下几行:
关键部分是包含此 1px 设置。当然,drawable 使用渐变(1px),这不是最佳解决方案。我尝试使用中风但没有成功。 (您似乎没有使用样式,因此只需为 ListView 添加 android:dividerHeight="1px" 属性即可。
This is a workaround, but works for me:
Created res/drawable/divider.xml as follows:
And in styles.xml for listview item, I added the following lines:
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.
添加
android:dividerHeight="1px"
它将起作用:Add
android:dividerHeight="1px"
and it will work:您遇到的问题源于您缺少 android:dividerHeight ,这是您所需要的,并且您试图在可绘制对象中指定线条粗细,而对于某些情况,您无法使用分隔线来做到这一点奇怪的原因。本质上,为了让您的示例正常工作,您可以执行以下操作:
将您的可绘制对象创建为矩形或直线,两者都可以工作,您只是无法尝试在其上设置任何尺寸,因此:
或者:
然后创建一个自定义样式(只是一个偏好,但我希望能够重用东西)
最后使用自定义样式声明您的列表视图:
我假设您知道如何使用这些片段,如果不知道的话请告诉我。基本上你的问题的答案是你不能在可绘制对象中设置分隔线厚度,你必须在那里保留未定义的宽度并使用 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:
OR:
Then create a custom style (just a preference but I like to be able to reuse stuff)
Finally declare your list view using the custom style:
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.
从文档中:
看起来必须调用
setDividerHeight()
才能在没有固有高度的情况下显示分隔线From the doc:
Looks like
setDividerHeight()
must be called in order for the divider to show up if it has no intrinsic height您的
@drawable/list_divide
应如下所示:在您的版本中,您提供
android:width="1dp"
,只需将其更改为android:height= “1dp”
它应该可以工作!Your
@drawable/list_divide
should look like this:In your version you provide an
android:width="1dp"
, simply change it to anandroid:height="1dp"
and it should work!来自 doc:
基本上,您需要将一个名为
list_divider.xml
的文件放入res/drawable/
中,以便您可以通过R.drawable.list_divider
访问它>;如果您可以通过这种方式访问它,那么您可以在ListView
的 XML 中使用android:divider="@drawable/list_divider"
。From the doc:
basically, you'll need to put a file named
list_divider.xml
inres/drawable/
so you can access it asR.drawable.list_divider
; if you can access it that way, then you can useandroid:divider="@drawable/list_divider"
in the XML forListView
.有些人可能会遇到一条实线。我通过将
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.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"
我有同样的问题。然而,在我原来的 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 adividerHeight
parameter. I set it to2dp
in thevalues-mdpi
folder but1dp
in thevalues-hdpi
etc folders.您忘记了分隔符 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"
设置 android:dividerHeight="1dp"
Set
android:dividerHeight="1dp"