【Android 開發】Android中自定義ListView中Item間的分割線

============================================================================================================================================================================================================================================================================

相信大家在做ListView時,Item之間需要添加分割線的需求。今天帶大家來實現下ListView中在Item間添加分隔線

============================================================================================================================================================================================================================================================================

1.不顯示分割線只要在ListView控件中添加android:footerDividersEnabled="false"即可。

  1. <ListView  
  2.     android:id="@+id/local_groups_list"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:footerDividersEnabled="false" />  




2.改變ListView的分割線顏色和寬度,需要在佈局中定義android:dividerandroid:dividerHeight屬性。

  1. <ListView  
  2.     android:id="@+id/local_groups_list"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:divider="@color/divider_color"  
  6.     android:dividerHeight="1px" />  
註明:ListView中每個Item項之間都有分割線,設置Android:footerDividersEnabled表示是否顯示分割線,此屬性默認爲true。

=======================================================================================



 <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:divider="@drawable/list_item_divider"
        android:dividerHeight="1px"
        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

list_item_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:drawable="@color/colorPrimary"
       android:insetLeft="15dp"
    />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#63a219</color>
</resources>

======================================================================================

自定義虛線的listView分割線 

==========================================================================================================================================

<ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:divider="@drawable/list_item_dash"
        android:dividerHeight="5dp"
        android:paddingLeft="5px"
        android:paddingRight="5px"
        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

list_item_dash.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="line">
    <!-- 顯示虛線,破折線的寬度爲dashWith,空隙的寬度爲dashGap, darkgray -->

    <stroke
        android:width="1dp"
        android:color="#63a219"
        android:dashGap="3dp"
        android:dashWidth="6dp"/>
    <!-- 虛線的高度 -->
    <size android:height="5dp"/>
</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

如果虛線加載不出來,在 AndroidManifest.xml文件中,把硬件加速功能關掉就可以了,android:hardwareAccelerated=”false”。 

歡迎學習交流,覺得還行就定下咯奮鬥





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