【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”。 

欢迎学习交流,觉得还行就定下咯奋斗





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值