listView加边框

本文介绍如何在Android中为ListView设置边框,包括边框宽度、颜色,以及实现圆角背景。通过在XML drawable文件中定义shape,可以自定义ListView的圆角效果和边框样式。
摘要由CSDN通过智能技术生成

<ListView

                android:id="@+id/listview2"
                android:layout_width="600dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:divider="@color/gray"
                android:dividerHeight="1dp"
                android:background="@drawable/listview_round_corner_bg"
                android:cacheColorHint="#00000000" />

 

 

在drawable文件夹中添加listview_round_corner_bg.xml文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

   

<!-- 这里可以自己设置

要在Android ListView中添圆角边框,可以使用以下步骤: 1. 创建一个drawable资源文件,例如“listview_border.xml”,并在其中定义一个shape元素,设置圆角和边框属性。 2. 在ListView的布局文件中,将背景属性设置为刚刚创建的drawable资源文件。 3. 在ListView的适配器中,为每个列表项设置背景颜色或背景图片,以便它们显示圆角边框。 以下是一个示例drawable资源文件的代码: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/white" /> <corners android:radius="10dp" /> <stroke android:width="2dp" android:color="@android:color/black" /> </shape> ``` 在ListView的布局文件中,可以这样设置背景属性: ``` <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/listview_border" /> ``` 在适配器中,可以这样设置每个列表项的背景: ``` @Override public View getView(int position, View convertView, ViewGroup parent) { // inflate the layout for each list item View listItemView = convertView; if (listItemView == null) { listItemView = LayoutInflater.from(getContext()).inflate( R.layout.list_item, parent, false); } // set the background color or image for this list item listItemView.setBackgroundResource(R.drawable.list_item_background); // set the text and image for this list item TextView textView = (TextView) listItemView.findViewById(R.id.textview); ImageView imageView = (ImageView) listItemView.findViewById(R.id.imageview); textView.setText(getItem(position).getText()); imageView.setImageResource(getItem(position).getImageResource()); return listItemView; } ``` 在这个示例中,我们为每个列表项设置了一个名为“list_item_background”的drawable资源文件,它定义了一个圆角矩形的背景。您可以根据自己的需要更改此文件的属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值