我想更改 ListView
分隔线的颜色。任何帮助,将不胜感激。
您可以使用 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>
或者你可以编码:
int[] colors = {0, 0xFFFF0000, 0}; // red for the example
myList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
myList.setDividerHeight(1);
希望能帮助到你
对于单色线,请使用:
list.setDivider(new ColorDrawable(0x99F10529)); //0xAARRGGBB
list.setDividerHeight(1);
重要的是 DividerHeight 设置在分隔符之后,否则您将一无所获。
您还可以使用以下方法从资源中获取颜色:
dateView.setDivider(new ColorDrawable(_context.getResources().getColor(R.color.textlight)));
dateView.setDividerHeight(1);
@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" />
有两种方法可以做到这一点:
您可以在布局 xml 文件中设置 android:divider="#FFCCFF" 的值。有了这个,你还必须像这样指定分隔线的高度 android:dividerHeight="5px"。
在您的 xml 文件中使用以下代码
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#000000"
android:dividerHeight="1dp">
</ListView>
以编程方式使用
// 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>
将 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"/>
android:divider
中指定一个Drawable
资源。现有的分隔线是渐变。px
单位来定义尺寸,而是使用dp