2010-03-03 59 views

Trả lời

716

Bạn có thể đặt giá trị này trong tệp xml bố cục bằng cách sử dụng android:divider="#FF0000". Nếu bạn đang thay đổi màu sắc/drawable, bạn phải thiết lập/thiết lập lại chiều cao của divider quá.

<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> 
+11

Bạn cũng nên có thể xác định một ' Tài nguyên có thể draw' trong 'android: divider' là tốt. Dải phân cách hiện tại là một gradient. – CommonsWare

+0

Dải phân cách gradient hiện tại ở đâu? – ninjasense

+58

Nếu bạn làm điều đó trong XML, hãy chắc chắn để xem chiều cao cũng bằng cách sử dụng android: dividerHeight nếu không bạn sẽ không nhận được dòng –

159

Hoặc bạn có thể mã hóa nó:

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

Hy vọng nó giúp

+0

Hoàn hảo, các mục của tôi nằm trên nền gradient màu đỏ và hiệu ứng của bạn khiến chúng trở nên tuyệt vời !! – Darkendorf

+1

nếu bạn mở rộng ListActivity, thay thế danh sách của tôi bằng getListView() – Aziz

83

Đối với việc sử dụng dòng màu duy nhất:

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

Điều quan trọng là DividerHeight được thiết lập sau khi divider, nếu không bạn sẽ không nhận được gì.

+1

Cảm ơn bạn, tôi đã gọi setDividerHeight() trước khi setDivider() và không có dải phân cách nào được hiển thị. –

+3

Nhận xét rất hữu ích về thứ tự các hoạt động. Tôi chỉ mất 2 giờ để làm cho nó hoạt động. Thiết kế đẹp, Android. –

10

Bạn cũng có thể nhận được các màu sắc từ các nguồn lực của bạn bằng cách sử dụng:

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

Sử dụng android:divider="#FF0000"android:dividerHeight="2px" cho ListView.

<ListView 
android:id="@android:id/list" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:divider="#0099FF" 
android:dividerHeight="2px"/> 
9

Phiên bản XML cho @Asher Aslan hiệu ứng nguội.

<?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> 

tên cho hình dạng như: list_driver.xml dưới thư mục drawable

<ListView 
     android:id="@+id/category_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:divider="@drawable/list_driver" 
     android:dividerHeight="5sp" /> 
4

Có hai cách để làm điều tương tự:

  1. Bạn có thể thiết lập giá trị của android : divider = "# FFCCFF" trong tệp xml bố cục. Với điều này, bạn cũng phải chỉ định chiều cao của dải phân cách như thế này 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> 
    
  2. Bạn cũng có thể làm điều này bằng lập trình ...

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

Sử dụng dưới mã trong tập tin xml của bạn

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

Tốt nhất là giải thích một chút về lý do giải pháp của bạn hoạt động. Các câu trả lời chỉ có mã có thể khắc phục vấn đề nhưng điều đó không nhất thiết phải trả lời câu hỏi của người hỏi. – SuperBiasedMan

Các vấn đề liên quan