ChatGPT解决这个技术问题 Extra ChatGPT

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

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


S
Sebastian Hojas

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

或者你可以编码:

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

希望能帮助到你


完美,我的物品在红色渐变背景上,你的效果让它们变得华丽!
如果您扩展 ListActivity,请将 mylist 替换为 getListView()
h
htafoya

对于单色线,请使用:

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

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


谢谢,我在 setDivider() 之前调用了 setDividerHeight() 并且没有显示分隔符。
关于操作顺序的非常有用的评论。我只花了 2 个小时试图让它工作。不错的设计,安卓。
B
BitBlt

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

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

P
Peter Nguyen

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

该形状的名称为:可绘制文件夹下的 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" />

u
user692942

有两种方法可以做到这一点:

您可以在布局 xml 文件中设置 android:divider="#FFCCFF" 的值。有了这个,你还必须像这样指定分隔线的高度 android:dividerHeight="5px"。 你也可以通过编程来做到这一点... ListView listView = getListView (); ColorDrawable myColor = new ColorDrawable(this.getResources().getColor(R.color.myColor)); listView.setDivider(myColor); listView.setDividerHeight();


R
Rory McCrossan

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

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

最好解释一下您的解决方案为何有效。仅代码答案可能会解决问题,但这并不一定能回答提问者的问题。
C
Chanaka Weerasinghe

以编程方式使用

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

V
Vettiyanakan

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