2011-11-04 18 views

回答

12

製作一個9片png,其左側和右側有透明像素。例如,一個53x4 .9.png在任何一側都有25個透明像素(+像素到9個貼圖)會將1個像素拉伸出來,所以每邊都有25個像素。

0

你應該能夠調用

mListView.setDivider(Drawable d);

,並通過它可抽拉,您可以在您的RES /繪製的文件夾。如果你想讓它幾乎全部穿過你,你可以製作一個9補丁,其中包含一個水平線,並在左右兩側保留儘可能多的透明度。並將其設置爲拉伸線的中間部分。

3

如果你不想讓9補丁,那麼您可以在LIST_ITEM的XML代碼插入

<View android:layout_width="fill_parent" android:layout_height="1dp" android:layout_marginTop="4dp" android:background="#33B5E5" />

。它創建一條藍線,你可以很容易地控制這條線的寬度。 爲了使這個成功,你將不得不禁用列表視圖的分隔符。給出here

+0

離開'width'在'match_parent'並設置左邊或右邊距。 –

49

A RecyclerView現在比現在使用ListView更優選。有關如何在RecyclerView中設置分頻器的寬度,請參見this Q&A

使用<inset>

繪製/ list_divider.xml

<inset xmlns:android="http://schemas.android.com/apk/res/android" 
    android:insetLeft="10dp" 
    android:insetRight="10dp" > 

    <shape android:shape="rectangle" > 
     <solid android:color="@color/list_divider_color" /> 
    </shape> 

</inset> 

而在你的佈局:

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:cacheColorHint="#00000000" 
    android:divider="@drawable/list_divider" 
    android:dividerHeight="1dp" > 
</ListView> 

enter image description here

來源:

+0

我試過了。但它將默認分隔符覆蓋在默認分隔符上。任何想法爲什麼? – Zen

+0

@Surag對於offtopic感到抱歉,但在有關'ListView'棄用的文檔中我沒有發現任何提及。我理解你在這些話背後的想法,但我認爲它可能會讓某些人感到困惑。不過,很好的答案。 – Viacheslav

+1

@Viacheslav,謝謝。我糾正了我的措辭。 – Suragch