如何更改Android ListView分隔线的颜色?

407

我想要改变 ListView 分隔线的颜色。

9个回答

770

您可以在布局xml文件中使用android:divider ="#FF0000"设置此值。 如果更改颜色/ drawable,则还必须设置/重置分隔符的高度。

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <ListView 
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:divider="#FFCC00"
    android:dividerHeight="4px"/>

</LinearLayout>

11
android:divider中,你也可以指定一个Drawable资源。现有的分隔符是一个渐变。 - CommonsWare
62
如果您在XML中进行操作,请确保使用android:dividerHeight查看高度,否则将无法获得分割线。 - Eric Novins
8
根据我的经验,将“应该重置分隔线的高度”翻译为“必须设置分隔线的高度”。 - dpjanes
45
我不建议在Android中使用px单位来定义大小,改用dp单位。 - Marek Sebera
12
在这种特定情况下使用像素似乎有很好的理由。请参见:https://dev59.com/mG865IYBdhLWcg3wHKzu#12061612 - greg7gkb
显示剩余8条评论

165

或者您可以编写代码:

int[] colors = {0, 0xFFFF0000, 0}; // red for the example
myList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
myList.setDividerHeight(1);

太完美了,我的物品原本是在红色渐变背景上,而你的效果让它们变得华丽无比!! - Darkendorf
1
如果你扩展了ListActivity,请用getListView()替换mylist。 - Aziz

90

对于单色线,请使用:

list.setDivider(new ColorDrawable(0x99F10529));   //0xAARRGGBB
list.setDividerHeight(1);

很重要的一点是在设置分隔线之后再设置DividerHeight,否则你将得不到任何东西。


1
谢谢,我在调用setDivider()之前调用了setDividerHeight(),所以没有显示分隔线。 - Andreas Klöber
4
关于操作顺序的评论非常有帮助。我刚刚花了2个小时才让它正常工作。安卓设计得很好。 - Nick Frolov

13

您还可以通过以下方式从资源中获取颜色:

dateView.setDivider(new ColorDrawable(_context.getResources().getColor(R.color.textlight)));
dateView.setDividerHeight(1);

10

为 @Asher Aslan 酷炫效果设计的 XML 版本。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="180"
        android:startColor="#00000000"
        android:centerColor="#FFFF0000"
        android:endColor="#00000000"/>

</shape>

将该形状命名为:在drawable文件夹下的list_driver.xml

<ListView
        android:id="@+id/category_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:divider="@drawable/list_driver"
        android:dividerHeight="5sp" />

6

有两种方法可以做到同样的事情:

  1. You may set the value of android:divider="#FFCCFF" in layout xml file. With this you also have to specify height of divider like this android:dividerHeight="5px".

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
      <ListView 
      android:id="@+id/lvMyList"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:divider="#FFCCFF"
      android:dividerHeight="5px"/>
    
    </LinearLayout>
    
  2. You may also do this by programmatically...

    ListView listView = getListView();
    ColorDrawable myColor = new ColorDrawable(
        this.getResources().getColor(R.color.myColor)
    );
    listView.setDivider(myColor);
    listView.setDividerHeight();
    

2

在您的xml文件中使用下面的代码

<ListView 
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#000000" 
    android:dividerHeight="1dp">
</ListView> 

3
最好解释一下为什么您的解决方案有效。仅使用代码来回答可能会修复问题,但这并不一定回答了提问者的问题。 - SuperBiasedMan

1
使用编程方式。
           // Set ListView divider color
            lv.setDivider(new ColorDrawable(Color.parseColor("#FF4A4D93")));

            // set ListView divider height
            lv.setDividerHeight(2);

使用 XML。
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <ListView 
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:divider="#44CC00"
    android:dividerHeight="4px"/>

</LinearLayout>

0
使用 android:divider="#FF0000"android:dividerHeight="2px" 来设置 ListView 的分割线。
<ListView 
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#0099FF"
android:dividerHeight="2px"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接