【发布时间】:2011-01-23 06:22:04
【问题描述】:
我想更改ListView 分隔线的颜色。任何帮助将不胜感激。
【问题讨论】:
我想更改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>
【讨论】:
android:divider 中指定Drawable 资源。现有的分隔线是渐变。
px单位来定义尺寸,而是使用dp
或者你可以编码:
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" />
【讨论】:
有两种方法可以做到这一点:
您可以在布局 xml 文件中设置 android:divider="#FFCCFF" 的值。 有了这个,你还必须像这样 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>
您也可以通过编程方式...
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);
使用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"/>
【讨论】: