Как изменить цвет разделителя в списке?

Мне нужно изменить цвет разделителя в списке. Код, который я использую для выполнения этого, приведен ниже:

<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 пиксель. Что я делаю неправильно?

Обновление: ListItem

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

Ответ 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.

Обновление:

Просто проверьте свой макет списка, вы упоминали 1px для layout_width и layout_height, и вы устанавливаете 4px для dividerHeight.

Ответ 2

Я думаю, проблема в ваших ListView layout_width и layout_height.

Установите layout_width="fill_parent" и layout_height="wrap_content"

В противном случае

Способы установки цвета и высоты разделителя в списке просмотра

  • Вы можете установить это значение в XML файле макета с помощью android:divider="#FF0000".

  • Вы также должны установить / reset высоту делителя при его изменении.

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

  • Вы также можете указать ресурс Drawable в android:divider.

  • Вы можете запрограммировать его:

    int[] colors = {0, 0xFFFF0000, 0}; // red for the example
    myList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
    myList.setDividerHeight(1);
    
  • Вы можете сделать, например

    Способ 1:

В 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);

Способ 2:

В 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:

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