【问题标题】:How to change the divider color in the listview?如何更改列表视图中的分隔线颜色?
【发布时间】:2011-09-26 13:08:15
【问题描述】:

我需要更改列表视图中的分隔线颜色。下面引用了我用来完成此操作的代码:

<ListView
    android:id="@+id/restaurant_list_widget"
    android:layout_width="1px"
    android:layout_height="1px"
    android:layout_weight="1" android:background="@drawable/list"
    android:divider="#FFFFFF"
    android:dividerHeight="4px">
</ListView>  

我仍然得到一个黑色 1 像素宽的分隔线。我做错了什么?

更新:列表项

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:background="@color/list_background">
        <TextView
            android:id="@+id/restaurant_list_item_name"
            android:layout_marginBottom="4dp"
            android:textStyle="bold"
            android:textSize="15dp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:textColor="#fff"
            android:text="Restaurant Name Goes Here"></TextView>
        <TextView
            android:id="@+id/restaurant_list_item_detail"
            android:textSize="10dp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"></TextView>    
</LinearLayout>

【问题讨论】:

  • 可以参考这个answer
  • 您能否发布另一个指定列表项的 xml。检查你在那里设置的背景颜色。

标签: android


【解决方案1】:

我已经试过了:

 <ListView 
    android:id="@+id/ListView01" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@color/redBackground"
    android:dividerHeight="1dip">
 </ListView>

颜色值在colors.xml中:

<color name="redBackground">#C60202</color>

而且它工作正常,并将分隔线颜色显示为红色,高度为 1dip。

更新:

只需检查您的列表视图布局,您已经为 layout_widthlayout_height 提到了 1px,而您正在为 dividerHeight 设置 4px。

【讨论】:

    【解决方案2】:

    我认为问题出在您的ListViewlayout_widthlayout_height

    设置layout_width="fill_parent"layout_height="wrap_content"

    否则

    在 Listview 中设置分隔线颜色和高度的方法

    1. 您可以使用 android:divider="#FF0000" 在布局 xml 文件中设置此值。

    2. 您还应该在修改分隔线时设置/重置分隔线的高度。

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

    3. 您也可以在android:divider 中指定Drawable 资源。

    4. 你可以编码:

      int[] colors = {0, 0xFFFF0000, 0}; // red for the example
      myList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
      myList.setDividerHeight(1);
      
    5. 你可以这样做

      方法一:

    在 res/values/colors.xml 中,放入以下内容:

    <resources>
     <color name="sage">#cceebb</color>
    </resources>
    

    在您的 ListActivity-extending 类中,执行以下操作:

    ListView lv = getListView();
    ColorDrawable sage = new ColorDrawable(this.getResources().getColor(R.color.sage));
    lv.setDivider(sage);
    lv.setDividerHeight(1);
    

    方法二:

    在 res/values/colors.xml:

    <resources>
     <drawable name="sage">#cceebb</drawable>
    </resources>
    

    在你的扩展 ListActivity 的类中:

    ListView lv = getListView();
    ColorDrawable sage = new ColorDrawable(this.getResources().getColor(R.drawable.sage));
    lv.setDivider(sage);
    lv.setDividerHeight(1);
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      你只需要设置ListView的divider属性:

      android:divider="#FFCC00"
      

      【讨论】:

        【解决方案4】:

        您应该在 ListView 中添加以下代码:

           android:divider="@android:color/white"
           android:dividerHeight="0.2dp"
        

        【讨论】:

          【解决方案5】:

          另一种方法是在您的styles.xml 中添加样式标签

          <item name="android:divider">#B6B6B6</item>
          

          例如:

          <style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
              <item name="colorPrimaryDark">#303F9F</item>
              <item name="colorPrimary">#3F51B5</item>
              <item name="colorAccent">#FF5722</item>
              <item name="android:textColorPrimary">@color/textColorPrimary</item>
              <item name="android:textColorSecondary">@color/textColorSecondary</item>
              <item name="android:divider">#B6B6B6</item>
              <item name="android:windowBackground">@color/window_background</item>
          </style>
          

          【讨论】:

            【解决方案6】:

            您必须添加以下代码。

            <ListView
                    android:id="@+id/restaurant_list_widget"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:divider="#FFFFFF"
                    android:dividerHeight="0.5dp" />
            

            【讨论】:

              猜你喜欢
              • 2012-06-09
              • 1970-01-01
              • 2014-08-05
              • 1970-01-01
              • 2011-01-23
              • 2012-02-14
              • 1970-01-01
              • 1970-01-01
              • 2015-10-03
              相关资源
              最近更新 更多