【Android学习笔记】listview中去除分割线和每个item的间隔

这篇博客介绍了如何在Android中消除ListView的默认分割线以及调整每个item之间的间距。通过在XML布局中将`android:divider`设为`@null`可以去除分割线,而通过在item外部包裹一个Relative Layout并设置padding来实现item间的间隔控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用adapter listview的时候,系统会自动加上一条分割线,有时候会觉得很难看,把dividerHeigth这个高度设置为0都是不可以的,但是在xml中把divider这个属性设置为空就搞掂啦~

[html]  view plain copy
  1. <ListView  
  2.         android:id="@+id/commentListView"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="match_parent"  
  5.         android:fadingEdge="none"  
  6.         android:cacheColorHint="@null"  
  7.         android:divider="@null"  
  8.         android:layout_margin="5dip">  
  9.         </ListView>  

      然后又发现了另一种情况,为什么把margin设置了5个dip,但是没效果?问题是你设置的是整个listview的边界,而不是每个item的边界,

      这样的话我暂时还没找到可以用一句代码可以控制的,但是还是有办法可以解决,就是在item外面再套一个relativelayout,里面设置padding。

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_height="wrap_content"  
  4.     android:layout_width="wrap_content"  
  5.     android:paddingTop="5dip">  
  6.   
  7. <RelativeLayout  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="65dip"  
  10.     android:padding="5dip"  
  11.     android:layout_centerVertical="true"  
  12.     android:background="@color/whitesmoke">  
  13.   
  14.   
  15.         <ImageView  
  16.             android:id="@+id/account_img"  
  17.             android:layout_width="45dp"  
  18.             android:layout_height="45dp"  
  19.             android:scaleType="centerCrop"  
  20.             android:layout_marginLeft="@dimen/header_img_margin_left"  
  21.             android:src="@drawable/account_avatar" />  
  22.   
  23.     <TextView  
  24.         android:id="@+id/account_name"  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_toRightOf="@id/account_img"  
  28.         android:layout_alignParentTop="true"  
  29.         android:padding="2dp"  
  30.         android:textSize="20dip"  
  31.         android:text="kam"  
  32.         android:textColor="@android:color/black" />  
  33.   
  34.     <TextView  
  35.     android:id="@+id/account_contents"  
  36.     android:layout_width="wrap_content"  
  37.     android:layout_height="wrap_content"  
  38.     android:layout_below="@id/account_name"  
  39.     android:layout_alignLeft="@id/account_name"  
  40.     android:padding="2dp"  
  41.     android:textSize="@dimen/text_size"  
  42.     android:text="so cool~"  
  43.     android:textColor="@color/gray" />  
  44.   
  45.     <TextView  
  46.         android:id="@+id/comment_time"  
  47.         android:layout_width="wrap_content"  
  48.         android:layout_height="wrap_content"  
  49.         android:layout_alignParentBottom="true"  
  50.         android:layout_alignParentRight="true"  
  51.         android:padding="2dp"  
  52.         android:textSize="@dimen/text_size"  
  53.         android:layout_marginRight="@dimen/header_img_margin_left"  
  54.         android:text="1 hour ago"  
  55.         android:textColor="@color/gray" />  
  56.   
  57. </RelativeLayout>  
  58. </RelativeLayout>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值