ListView的divider設置

android中ListView默認的分割線是填充滿整個listview的寬度的,然後設計師需要的偏偏就是不填充滿的狀態,找了相關資料,寫了個demo驗證下。


 

當然最簡單的實現方式就是用圖片了,但是圖片就要涉及到分辨率,那用顏色填充會比較直接。

在drawable中定義我需要的分割線main_list_divider_line.xml

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.         <item  
  4.         android:left="10dp"  
  5.         android:right="10dp">  
  6.         <shape android:shape="rectangle" >  
  7.             <solid android:color="#33000000" />  
  8.         </shape>  
  9.     </item>  
  10. </layer-list>  

 其中android:left表明其距離左邊的距離,android:right表示距離右邊的距離,android:shape="rectangle"表示是用一個矩形填充,android:color="#33000000"填充的顏色。

然後在配置listview的地方設置分割線爲上述填充物即可

Xml代碼  收藏代碼
  1. <ListView  
  2.     android:id="@+id/main_list"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:divider="@drawable/<span style="color: #ff00ff;">main_list_divider_line</span>"  
  6.     android:dividerHeight="1px"  
  7.     android:layout_marginTop="14dp" >  
  8. </ListView>  

 
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章