============================================================================================================================================================================================================================================================================
相信大家在做ListView時,Item之間需要添加分割線的需求。今天帶大家來實現下ListView中在Item間添加分隔線
============================================================================================================================================================================================================================================================================
1.不顯示分割線只要在ListView控件中添加android:footerDividersEnabled="false"即可。
- <ListView
- android:id="@+id/local_groups_list"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:footerDividersEnabled="false" />
2.改變ListView的分割線顏色和寬度,需要在佈局中定義android:divider和android:dividerHeight屬性。
- <ListView
- android:id="@+id/local_groups_list"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:divider="@color/divider_color"
- android:dividerHeight="1px" />
=======================================================================================
<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”。
歡迎學習交流,覺得還行就定下咯