android listview圆角边框
时间: 2023-04-30 14:00:22 浏览: 92
要在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资源文件,它定义了一个圆角矩形的背景。您可以根据自己的需要更改此文件的属性。