我想要改变 ListView
分隔线的颜色。
您可以在布局xml文件中使用android:divider ="#FF0000"
设置此值。 如果更改颜色/ drawable,则还必须设置/重置分隔符的高度。
<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>
将该形状命名为:在drawable文件夹下的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" />
有两种方法可以做到同样的事情:
You may set the value of android:divider="#FFCCFF" in layout xml file. With this you also have to specify height of divider like this 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>
You may also do this by programmatically...
ListView listView = getListView();
ColorDrawable myColor = new ColorDrawable(
this.getResources().getColor(R.color.myColor)
);
listView.setDivider(myColor);
listView.setDividerHeight();
在您的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);
<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
资源。现有的分隔符是一个渐变。 - CommonsWarepx
单位来定义大小,改用dp
单位。 - Marek Sebera