2010-03-03 81 views

回答

716

您可以使用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> 
+11

你也應該能夠指定一個' 'android:divider'中的Drawable資源也是如此。現有的分頻器是一個漸變。 – CommonsWare 2010-03-03 16:27:53

+0

哪裏是現有的梯度分配器? – ninjasense 2010-10-21 21:52:38

+58

如果你在XML中使用它,請確保使用android:dividerHeight查看高度,否則你將無法獲得線條 – 2011-06-28 22:57:01

159

或者你可以編寫它:

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

希望它可以幫助

+0

完美,我的物品是在一個紅色的漸變背景,你的效果使他們宏偉! – Darkendorf 2012-12-21 07:44:08

+1

如果你擴展ListActivity,用getListView()替換mylist() – Aziz 2014-04-24 14:40:43

83

對於單一色線使用:

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

是DividerHeight之後的設置是非常重要的除法器,否則你不會得到任何東西。

+1

謝謝,我在setDivider()之前調用了setDividerHeight(),並且沒有顯示分隔符。 – 2012-08-24 14:42:28

+3

對操作順序非常有幫助的評論。我只花了2個小時試圖讓它工作。漂亮的設計,Android。 – 2014-07-10 09:52:26

10

您也可以通過從你的資源的顏色:

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

使用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"/> 
9

XML版本@Asher Aslan酷效果。

<?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" /> 
4

有兩種方法做同樣的:

  1. 你可以設置了Android的價值:divider =「#FFCCFF」 in layout xml file。 有了這個你也必須指定這樣的分隔線的高度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. 您還可以通過編程方式做到這一點...

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

使用下面的代碼在XML文件中

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

最好解釋一下爲什麼你的解決方案有效。只回答代碼可能會解決問題,但這並不一定回答提問者的問題。 – SuperBiasedMan 2015-05-15 10:03:18

相關問題