【问题标题】:How to change color of Android ListView separator line?如何更改 Android ListView 分隔线的颜色?
【发布时间】:2011-01-23 06:22:04
【问题描述】:

我想更改ListView 分隔线的颜色。任何帮助将不胜感激。

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    您可以使用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>
    

    【讨论】:

    • 您还应该能够在android:divider 中指定Drawable 资源。现有的分隔线是渐变。
    • 如果您在 XML 中执行此操作,请确保使用 android:dividerHeight 也可以查看高度,否则您将看不到任何线条
    • 根据我的经验,将“应该重置分隔线的高度”阅读为“必须设置分隔线的高度”
    • 我不建议在Android中使用px单位来定义尺寸,而是使用dp
    • 在这种特定情况下使用 px 似乎有充分的理由。见:stackoverflow.com/a/12061612/10505
    【解决方案2】:

    或者你可以编码:

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

    希望对你有帮助

    【讨论】:

    • 完美,我的物品在红色渐变背景上,您的效果让它们非常棒!!
    • 如果扩展ListActivity,将mylist替换为getListView()
    【解决方案3】:

    对于单色线使用:

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

    DividerHeight 设置在分隔符之后很重要,否则您将一无所获。

    【讨论】:

    • 谢谢,我在 setDivider() 之前调用了 setDividerHeight() 并且没有显示分隔符。
    • 关于操作顺序的非常有用的评论。我只花了 2 个小时试图让它工作。不错的设计,Android。
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      @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. 您可以在布局 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>
          
        2. 您也可以通过编程方式...

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

        【讨论】:

          【解决方案7】:

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

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

          【讨论】:

          • 最好解释一下您的解决方案为何有效。仅代码答案可能会解决问题,但不一定能回答提问者的问题。
          【解决方案8】:

          以编程方式使用

                     // 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>
          

          【讨论】:

            【解决方案9】:

            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"/>
            

            【讨论】:

              猜你喜欢
              • 2013-09-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-08-05
              • 1970-01-01
              • 2012-06-09
              • 1970-01-01
              相关资源
              最近更新 更多