【问题标题】:How do you put a border around a ListView?如何在 ListView 周围放置边框?
【发布时间】:2011-02-21 12:07:23
【问题描述】:

我想在我的列表视图周围加上几个像素宽的边框。我希望它绕过整个列表视图。我怎样才能做到这一点?谢谢

【问题讨论】:

    标签: android listview layout


    【解决方案1】:

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

    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>
      
    2. 然后将其设置为列表视图背景

      <ListView
          android:id="@id/android:list"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/border_ui" />
      

    【讨论】:

    • 嗨!谢谢,帮助了我。如果我将 android:layout_marginLeft="20dp" 放在列表视图中,右边界线就会消失。这是为什么呢?
    • @user1809923 需要更多信息来说明这一点,最好创建自己的问题!我的猜测是您声明它与父宽度匹配,然后将右侧 20dp 推离屏幕。
    • 我已经尝试过这个解决方案,并且如果我在 lsitview 中有项目,边框没有显示同样的问题。我认为问题在于这些项目是在背景上绘制的,因此不可见。
    • @JanZiesse 尝试从列表项中删除背景,然后查看它是否显示。
    • 谢谢..为我工作。这应该是选择的答案。
    【解决方案2】:

    为此,首先获取 LinearLayout 并为该线性布局分配一些颜色,然后在该线性布局中获取一个列表视图。为列表视图设置android:layout_margin="10dp" 属性。这意味着在所有 4 个面上都将留下 10dp 的空间。这显示为列表视图的边框。

    【讨论】:

    • 这不是一个准确的答案...它只是一种解决方法。
    【解决方案3】:

    最简单的方法:

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"       
        android:divider="#FFCC00"
        android:dividerHeight="2dp"
        android:layout_weight="1" />
    

    【讨论】:

    • 这只会修改列表视图项目的分隔符。它不会在整个 Listview 周围绘制边框。
    • 适用于listview 分隔符
    【解决方案4】:

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

    您应该使用9 Patch 图像。它们允许您创建您喜欢的任何类型的背景,包括边框。该链接解释了一切。验证这里是bordered list 的图像。

    Here 是我用来制作边框的 9 个补丁图像的图像。

    【讨论】:

    • 虽然它肯定是一种选择,但我不会说它在大多数情况下更容易或更好;原因是您需要创建 9 个补丁并创建不同的密度。与 XML 文件相比,这需要更多的工作和更大的 APK。也就是说,当您想要提供更复杂的边框时,它非常有用。
    • 确实需要一点时间,但我真的很享受这个过程。只是想知道 APK 中 4 个大约 1-3 KB 的图像文件会产生什么影响,当然除了会增加 4-12 KB 的大小。
    • 它不会对大小产生巨大影响(至少在单一上下文中),但跨平台的一致性不会像像素大小在一定密度范围内固定一样一致。调整大小或颜色的能力也更加丰富。使用 9 个补丁可能会有轻微的性能提升,但在大多数情况下不足以引起注意。
    【解决方案5】:

    虽然这个问题发布了很长时间,但希望它可以帮助新人!

    在你项目的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>
    

    现在将您的列表视图布局设置为背景。

        <?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>
    

    所以看起来如下:

    这里的边框将围绕您的总列表视图,而不是围绕每个视图。

    要为列表视图提供单独的边框,请执行以下操作:

    <?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>
    

    用户界面将如下所示:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2011-03-30
      • 2013-02-04
      相关资源
      最近更新 更多