#Android给ListView加边框

#Android给ListView加边框

新建一个drawable的xml文件:leftlist_bg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/leftlist_item_normal"/>
    <stroke android:width="1px" android:color="@color/split_line"/>
</shape>

然后在listview上应用

<ListView
    android:id="@+id/left_nav"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:paddingRight="1px"
    android:layout_marginBottom="40dp"
    android:background="@drawable/leftlist_bg>
</ListView>

注意:paddingRight="1px",如果不设置这个属性的话,listview有item的地方是没有边框的。

item的layout_widht=""match_parent"会盖住边框。所以要paddingRight 1个px。

转载于:https://my.oschina.net/skyhacker2/blog/368437

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以通过为ListView的单个单元格(也称为ListView的列表项)定义一个自定义布局来为其添边框。以下是一个示例布局文件: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/list_item_border"> <!-- 添背景,即边框 --> <!-- 添其他视图,例如文本、图像等 --> <TextView android:id="@+id/item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="List Item Text" android:padding="10dp"/> </LinearLayout> ``` 在此示例布局中,我们使用LinearLayout作为根布局,并为其设置了一个背景(@drawable/list_item_border),该背景定义了单元格的边框。您可以使用其他布局替换LinearLayout,具体取决于您的需求。 然后,您需要在ListView的适配器中使用此自定义布局。例如,如果您使用ArrayAdapter,您可以按以下方式修改其构造函数: ```java ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, R.layout.list_item, R.id.item_text, data); ``` 这里,我们使用R.layout.list_item作为布局文件的资源ID,并使用R.id.item_text作为文本视图的资源ID。您需要将这些资源ID替换为您自己的布局文件中的相应ID。 请注意,您还需要在list_item_border.xml中定义边框的样式。以下是一个示例样式: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="1dp" android:color="#000000"/> <padding android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp"/> </shape> ``` 在此样式中,我们使用stroke元素定义了边框的宽度和颜色,并使用padding元素定义了单元格的内边距。您可以根据需要修改这些值。 希望这可以帮助您添ListView单元格的边框

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值