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

fruv7luv  于 2022-12-28  发布在  Android
关注(0)|答案(9)|浏览(180)

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

l7mqbcuq

l7mqbcuq1#

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

cpjpxq1n2#

或者您可以编写代码:

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

wvyml7n53#

对于单色线,用途:

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

在分隔符之后设置DividerHeight非常重要,否则您将得不到任何结果。

ee7vknir

ee7vknir4#

也可以使用以下命令从资源中获取颜色:

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

mm5n2pyu5#

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

s71maibg6#

有两种方法可以完成相同的操作:
1.您可以在布局xml文件中设置android:divider="#FFCFF”的值。使用该值时,您还必须指定分隔线的高度,如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>

1.您也可以通过编程方式来完成此操作...

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

uxh89sit7#

在xml文件中使用以下代码

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

n6lpvg4x8#

以编程方式使用

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

qxsslcnc9#

对列表视图使用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"/>

相关问题