如何在ListView周围放置一个边框?

47
我想在我的列表视图周围放置一个几个像素宽的边框,我希望它能够包围整个列表视图部分。我该如何做到这一点?谢谢。
5个回答

92
另一种方法是创建一个边框资源,这样可以重复使用它,而且还意味着您不需要创建额外的布局来实现它。

  1. 创建一个可绘制资源

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <!-- use this for transparent -->
   <!-- <solid android:color="#00000000" /> -->
   <!-- use this for a background colour -->
   <solid android:color="#FFF" />
   <stroke android:width="2dip" android:color="#FF0000" />
</shape>
然后将其设置为列表视图的背景。
<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border_ui" />

嗨!谢谢,帮了我很大的忙。如果我在ListView中添加android:layout_marginLeft="20dp",右边的边框为什么会消失呢? - user1809923
1
@user1809923 需要更多信息才能确定,最好创建自己的问题!我猜你正在声明它与父宽度匹配,然后将右侧20dp推出屏幕。 - Ben Neill
我尝试了这个解决方案,但是如果我在listview中有项目,则边框不会显示相同的问题。我认为问题在于项目被绘制在背景上,因此它不可见。 - Jan Ziesse
@JanZiesse 尝试将列表项的背景去掉,看看是否会显示出来。 - Ben Neill
谢谢,对我有用。这应该是被选中的答案。 - Parag Kadam
为了在列表中看到边框,我在ListView中添加了android:padding="3sp"。当然,如果您的边框更厚,您可能需要更多的填充。 - droideckar

18

首先,创建一个LinearLayout,为其分配一些颜色,并在该LinearLayout中创建一个ListView。为ListView设置android:layout_margin="10dp"属性,这意味着将在四个方向上留下10dp的空间,这将显示为ListView的边框。


这不是一个确切的答案...只是一个解决方法。 - Parag Kadam

15
最简单的方法:
<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"       
    android:divider="#FFCC00"
    android:dividerHeight="2dp"
    android:layout_weight="1" />

7
这只会修改列表视图项的分隔符,不会在整个列表视图周围绘制边框。 - Jan Ziesse
1
适用于“listview”分隔符的功能良好。 - T-D

1
尽管这个问题已经发布很久了,但希望能对新手有所帮助!!!
在您的项目的drawable文件夹下创建一个名为back.xml的文件!!!
   <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <!-- use this for transparent -->
    <!-- <solid android:color="#00000000" /> -->
    <!-- use this for a background colour -->
    <solid android:color="#FFF" />
    <stroke android:width="4dip" android:color="#FFCC00" />
</shape>

现在将其设置为您的listview布局的背景。
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/colorWhite"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header"
            android:textSize="40dp" />

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            **android:background="@drawable/back"** />
    </LinearLayout>

所以它看起来像以下内容:

listview with background border

这里的边框将围绕您的整个列表视图,而不是每个视图。
要为列表视图提供单独的边框,请执行以下操作:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorWhite"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Header"
        android:textSize="40dp" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@drawable/back"
        android:divider="#FFCC00"
        android:dividerHeight="2dp"/>
</LinearLayout>

UI将会出现如下:

listview with background of each view


1

有一个更简单的方法来在视图上创建边框和其他图形细节。

您应该使用9 Patch图像。它们允许您创建任何类型的背景,包括边框。链接中解释了所有内容。为了验证,这是一个bordered list的图片。

这里是我用来制作边框的9 Patch图像。


1
虽然这确实是一种选择,但在大多数情况下,我不会说它更容易或更好;原因是您需要创建9个补丁并为不同的密度创建。与XML文件相比,这需要更多的工作和更大的APK。话虽如此,当您想提供更复杂的边框时,它非常有用。 - Ben Neill
这确实需要一些时间,但我非常喜欢这个过程。只是想知道在 APK 中添加 4 个大约 1-3 KB 的图像文件会有什么影响,除了当然会增加 4-12 KB 的大小之外。 - Jack.Ramsden
1
它在大小上并没有太大的影响(至少在单一情境下),但是跨平台的一致性将不如像素大小在一系列密度上固定那样一致。调整大小或颜色的能力也更加麻烦。使用9个图块可能会带来轻微的性能提升,但在大多数情况下不足以引起注意。 - Ben Neill

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接