简体   繁体   中英

Set listView divider's width

I'm trying to use a listView with a divider and I was about to change divider's width so I looked in the web site and I found this solution :

Listview divider margin

When I tested in my app, I have no divider, but if I switch to use a simple color the divider appears again

This this my code :

my Activity layout :

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
    android:layout_width="match_parent"
    android:layout_height="80sp"
    android:elegantTextHeight="true"
    android:text="Paramètres"
    android:textSize="24sp"
    android:gravity="center"
    android:textColor="#ffffff"
    android:background="#191919"/>
<ListView
    android:id="@+id/list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:dividerHeight="2dp"
    android:divider="@drawable/list_divider">
</ListView>

</LinearLayout>

list_divider.xml

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="15dp"
android:insetRight="15dp" >

<shape
    android:shape="rectangle" >
    <stroke
        android:dashGap="1dp"
        android:dashWidth="1.5dip"
        android:width="2dp"
        android:color="#000000" />

    <size android:height="3dp"/>

</shape>

</inset>

You are giving wrong file name to your divider attribute.Change this line

android:divider="@drawable/list_devider"

To

android:divider="@drawable/list_divider"

because your list divider drawable file name is list_divider not list_devider

And I will suggest you to use px as a dividerHeight instead of dp

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

So I modified my listdivide.xml file to that, the it work :

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="16dp"
    android:insetRight="16dp">

    <shape>
        <solid android:color="#000000" />
        <corners android:radius="1dp" />
    </shape>

</inset>

But i still don't know why the first dosn't work :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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