Android ListView Divider


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>

but I can't see any divider.


1
I don't know why but the code is missing. here it is again:
oriharel

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

1
and the list divider is: <shape xmlns:android="schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="#8F8F8F" android:dashWidth="1dp" android:dashGap="1dp" /> </shape>
oriharel

use the code block (the 101010 icon) for inserting codes, especially XML/HTML/SGML code. I've fixed your post for now.
Lie Ryan

Maybe the reason of your problem is in your ListAdapter. Just try to return true of adapter's areAllItemsEnabled(). Or watch stackoverflow.com/questions/5587826/…
grine4ka

คำตอบ:


176

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


4
Wow. Saved my life. Should be part of Android's official guide to use "dip"
deeJ

I agree. They should at least mention that rule about px as an example of why they have it.
Joe Plante

6
On ldpi, 1dp = 0.75 pixels, therefore rounds down to 0. The divider then does not get drawn, which can pose problems for others. This also refers to the other statements in this thread complaining about using px altogether. This may or may not be the solution to his problem and it's up to him to declare whether or not it is
Joe Plante

18
1px is going to be incredibly small on an xxhdpi device, and at some point (as devices continue to get higher density) will be too small to see at all. Dip avoids this, and another solution for ldpi devices is to use 1px in the values-ldpi folder, and 1dip for higher densities.
eski

2
When I posted that response, xxhdpi I think was just coming out. However, your post makes sense, especially with xxxhdpi and possibly xxxxhdpi on the horizon
Joe Plante

55

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.


14
Or use 1dp for best practice.
Tristan Warner-Smith

2
Why are you using an angle of 270? List dividers are horizontal lines. 270 is a vertical gradient.
Christopher Perry

Isn't this a bug in Android? Shouldn't a line shape as divider just work?
Diederik

8
1px assets are the exception to the rule
Joe Plante

1
@TristanWarner-Smith this is incorrect. you need to use 1px in this case. see the accepted answer.
mpellegr

27

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>

15

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.


8

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


5

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!


5
android:height isn't a valid attribute for stroke, and width just means how wide the stroke is, not how long it is. The only reason your "solution" works is that Android doesn't recognize that height value.
Justin Buser

4

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.


I work with eclipse, so if I hadn't done that, the code wouldn't compile. so, with the file in place, still seems that the list view ignores my custom divider.
oriharel


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"


Yep. IF you are using 2 or more dp/dip, then you're okay. However, if you just want that 1-pixel divider, px is the way to go. You also get more screen real-estate with 1px, plus 1px usually looks better
Joe Plante

1

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.


1

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"


-1

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>
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.