¿Cómo cambiar el color de la línea de separación de Android ListView?
-
24-09-2019 - |
Pregunta
Quiero cambiar el color de ListView
línea separadora. Cualquier ayuda sería apreciada.
Solución
Puede establecer este valor en un archivo XML de diseño usando android:divider="#FF0000"
. Si está cambiando el color/dibujo, también debe configurar/restablecer la altura del divisor.
<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>
Otros consejos
O puede codificarlo:
int[] colors = {0, 0xFFFF0000, 0}; // red for the example
myList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
myList.setDividerHeight(1);
Espero eso ayude
Para una sola línea de color, uso:
list.setDivider(new ColorDrawable(0x99F10529)); //0xAARRGGBB
list.setDividerHeight(1);
Es importante que DividerHeight se establezca después del divisor, de lo contrario, no obtendrás nada.
También puede obtener los colores de sus recursos utilizando:
dateView.setDivider(new ColorDrawable(_context.getResources().getColor(R.color.textlight)));
dateView.setDividerHeight(1);
Versión XML para @asher aslan Cool Effect.
<?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>
Nombre para esa forma como: list_driver.xml en carpeta dibujable
<ListView
android:id="@+id/category_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@drawable/list_driver"
android:dividerHeight="5sp" />
Hay dos formas de hacer lo mismo:
Puede establecer el valor de Android: divider = "#ffccff" En el archivo XML de diseño. Con esto también tienes que especificar la altura del divisor como este 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>
También puede hacer esto mediante programación ...
ListView listView = getListView(); ColorDrawable myColor = new ColorDrawable( this.getResources().getColor(R.color.myColor) ); listView.setDivider(myColor); listView.setDividerHeight();
Use el código a continuación en su archivo XML
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#000000"
android:dividerHeight="1dp">
</ListView>
usando programéticamente
// Set ListView divider color
lv.setDivider(new ColorDrawable(Color.parseColor("#FF4A4D93")));
// set ListView divider height
lv.setDividerHeight(2);
Usando 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>
Usar android:divider="#FF0000"
y android:dividerHeight="2px"
Para ListView.
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#0099FF"
android:dividerHeight="2px"/>