首页 文章

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

提问于
浏览
382

我想改变 ListView 分隔线的颜色 . 任何帮助,将不胜感激 .

8 回答

  • 9

    您可以使用 android:divider="#FF0000" 在布局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="#FFCC00"
        android:dividerHeight="4px"/>
    
    </LinearLayout>
    
  • 11

    适用于@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" />
    
  • 0

    对于单色线使用:

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

    It's important that DividerHeight is set after the divider ,否则你什么都得不到 .

  • 160

    在xml文件中使用以下代码

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

    对于ListView使用 android:divider="#FF0000"android:dividerHeight="2px" .

    <ListView 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="#0099FF"
    android:dividerHeight="2px"/>
    
  • 2

    您还可以使用以下方法从资源中获取颜色:

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

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

    • 您可以在布局xml文件中设置 android:divider="#FFCCFF" 的值 . 有了这个,你还必须指定分隔符的高度,如 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>
    
    • 您也可以通过编程方式执行此操作...
    ListView listView = getListView();
    ColorDrawable myColor = new ColorDrawable(
        this.getResources().getColor(R.color.myColor)
    );
    listView.setDivider(myColor);
    listView.setDividerHeight();
    
  • 745

    或者你可以编码:

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

    希望能帮助到你

相关问题