【问题标题】:How to set space between listView Items in Android如何在Android中设置listView项目之间的空间
【发布时间】:2011-02-13 13:10:31
【问题描述】:

我尝试在 listView 上使用 marginBottom 在 listView Item 之间留出空间,但这些项目仍然连接在一起。

有可能吗?如果是,有具体的方法吗?

我的代码在下面

<LinearLayout
android:id="@+id/alarm_occurences"
android:layout_width="fill_parent" 
android:orientation="vertical"
android:layout_height="fill_parent"
android:background="#EEEEFF"
xmlns:android="http://schemas.android.com/apk/res/android">

<ListView
android:id="@+id/occurences"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

我的自定义列表项:

<com.android.alarm.listItems.AlarmListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:background="@drawable/alarm_item_background"
android:layout_marginBottom="10dp"    
>
<CheckedTextView     
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:textSize="20sp"
    android:textStyle="bold"
    android:typeface="serif"
    android:padding="10dp"

/>

</com.android.alarm.listItems.AlarmListItem>

在这种情况下如何在列表项之间设置间距?

【问题讨论】:

    标签: android android-layout android-listview


    【解决方案1】:

    @Asahi 几乎一针见血,但我只是想为以后可能通过 google 漂浮在这里的任何人添加一点 XML:

    <ListView android:id="@+id/MyListView"
      android:layout_height="match_parent"
      android:layout_width="match_parent"
      android:divider="@android:color/transparent"
      android:dividerHeight="10.0sp"/>
    

    由于某种原因,“10”、“10.0”和“10sp”等值都被 Android 拒绝为 dividerHeight 值。它需要一个浮点数和一个单位,例如“10.0sp”。正如@Goofyahead 所说,您还可以为此值使用与显示无关的像素(即“10dp”)。

    【讨论】:

    • 如果您还想在不拉伸的情况下显示分隔线,这将无济于事
    • 仅供参考 - 可以通过代码完成 - getListView().setDividerHeight(10)
    • 或 android:divider="@null"
    • @Sojunn 如果我需要类似的东西,我会尝试 9-patch。
    • @Sojunn 检查my answer。它会添加填充,但不会拉伸分隔线。
    【解决方案2】:

    也许ListView 的divider 或dividerHeight 属性可以解决您的问题。

    【讨论】:

      【解决方案3】:

      虽然 Nik Reiman 的解决方案确实有效,但我发现它并不是我想做的最佳解决方案。使用分隔线设置边距的问题是分隔线将不再可见,因此您无法使用它来显示项目之间的清晰边界。此外,它不会为每个项目添加更多“可点击区域”,因此如果您想让您的项目可点击并且您的项目很薄,那么任何人都很难点击一个项目,因为分隔线添加的高度不是项目的一部分。

      幸运的是,我找到了一个更好的解决方案,它既可以显示分隔线,又可以使用填充而不是边距来调整每个项目的高度。这是一个例子:

      列表视图

      <ListView
      android:id="@+id/listView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      />
      

      列表项

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="10dp"
          android:paddingTop="10dp" >
      
          <TextView
              android:id="@+id/textView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:text="Item"
              android:textAppearance="?android:attr/textAppearanceSmall" />
      
      </RelativeLayout>
      

      【讨论】:

      • 确实是一个更好的解决方案。谢谢。
      【解决方案4】:

      您应该将ListView 项目(例如your_listview_item)包装在其他布局中,例如LinearLayout 并为your_listview_item 添加边距:

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
          <your_listview_item
              android:id="@+id/list_item"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginTop="5dp"
              android:layout_marginBottom="5dp"
              android:layout_marginLeft="5dp"
              android:layout_marginRight="5dp"
          ...
          ...
          />
      </LinearLayout>
      

      这样,如果需要,您还可以在 ListView 项目的右侧和左侧添加空格。

      【讨论】:

      • 我想要左/右页边距,所以你的回答对我有帮助,但我不喜欢仅仅为了页边距而包装另一个布局的想法
      • 有几种方法.. 就布局性能而言,这是一个糟糕的方法.. 尝试考虑包含数十(或数百)个项目的列表 :)
      • @andrea.spot 对于包含数十或数百个项目的列表来说,这无关紧要,除非这是一次在屏幕上可见的项目数。 ListView 使用视图回收(如果您正确实现了适配器)。
      • 如果 ListView 项目有背景,这将不起作用
      【解决方案5】:

      我添加更多空间但保持水平线的解决方案是在res/drawable 文件夹中添加divider.xml 并在其中定义线条形状:

      divider.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="line" >
      
          <stroke
              android:width="1px"
              android:color="@color/nice_blue" />
      
      </shape>
      

      然后在我的列表中我引用我的分隔符如下:

      <ListView
          android:id="@+id/listViewScheduledReminders"
          android:layout_width="match_parent"
          android:layout_height="0dip"
          android:layout_marginBottom="@dimen/mediumMargin"
          android:layout_weight="1"
          android:divider="@drawable/divider"
          android:dividerHeight="16.0dp"
          android:padding="@dimen/smallMargin" >
      
      </ListView>
      

      通过增加和减少这个高度来注意android:dividerHeight="16.0dp",我基本上是在分隔线的顶部和底部添加更多的填充。

      我用这个页面作为参考:http://developer.android.com/guide/topics/resources/drawable-resource.html#stroke-element

      【讨论】:

        【解决方案6】:

        如果您想显示带有边距且不拉伸的分隔线 - 使用 InsetDrawable(大小必须采用格式,@Nik Reiman 对此表示):

        列表视图:

        <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"
            android:divider="@drawable/separator_line"
            android:dividerHeight="10.0px"/>
        

        @drawable/separator_line:

        <?xml version="1.0" encoding="utf-8"?>
        <inset xmlns:android="http://schemas.android.com/apk/res/android"
            android:insetLeft="5.0px"
            android:insetRight="5.0px"
            android:insetTop="8.0px"
            android:insetBottom="8.0px">
        
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">
                <gradient
                    android:startColor="@color/colorStart"
                    android:centerColor="@color/colorCenter"
                    android:endColor="@color/colorEnd"
                    android:type="linear"
                    android:angle="0">
                </gradient>
            </shape>
        </inset>
        

        【讨论】:

          【解决方案7】:

          你可以使用:

          android:divider="@null"
          android:dividerHeight="3dp"
          

          示例:

          <ListView
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/listView" android:layout_gravity="center_horizontal"
                  android:dividerHeight="3dp"
                  android:divider="@null" android:clickable="false"/>
          

          【讨论】:

          【解决方案8】:

          我意识到已经选择了一个答案,但我只是想分享一下当我遇到这个问题时最终对我有用的方法。

          我有一个 listView,其中 listView 中的每个条目都由其自己的布局定义,类似于 Sammy 在他的问题中发布的内容。我尝试了改变分隔高度的建议方法,但最终看起来并不太漂亮,即使有一个不可见的分隔线。经过一些实验后,我简单地将android:paddingBottom="5dip" 添加到 XML 文件中定义各个 listView 条目的最后一个 TextView 布局元素。

          这最终给了我通过使用android:layout_marginBottom 来实现的目标。我发现这个解决方案比尝试增加分隔高度更美观。

          【讨论】:

            【解决方案9】:

            对于我的申请,我是这样做的

             <ListView
                android:id="@+id/staff_jobassigned_listview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="@null"
                android:dividerHeight="10dp">
            
            </ListView>
            

            只需 set the divider to null 并为分隔线提供高度对我来说就是如此。

            示例:

            android:divider="@null"
            

            或

            android:divider="@android:color/transparent"
            

            这是结果

            【讨论】:

            • 我认为,android:divider="@android:color/transparent" 更好。
            【解决方案10】:

            你应该给定内边距,而不是给定边距:

            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:divider="@android:color/green"
                android:dividerHeight="4dp"
                android:layout_alignParentTop="true"
                android:padding="5dp" >
            
            </ListView>
            

            或

            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:paddingTop="2dp"
                android:divider="@android:color/green"
                android:dividerHeight="4dp"
                android:paddingLeft="1dp"
                android:paddingRight="1dp"
                android:paddingBottom="2dp"
                android:paddingStart="0dp"
                android:paddingEnd="0dp" >
            
            </ListView>
            

            【讨论】:

              【解决方案11】:

              使用 OP 现有代码(列表项已经有填充)的最简单解决方案是添加以下代码:

              listView.setDivider(new ColorDrawable(Color.TRANSPARENT));  //hide the divider
              listView.setClipToPadding(false);   // list items won't clip, so padding stays
              

              This 所以回答对我有帮助。

              注意:您可能会在旧平台上遇到列表项回收过快的错误,正如here 所问的那样。

              【讨论】:

              • 您可以将divider设置为null并将dividerHeight设置为您喜欢的任何值。
              • @Andy 我认为它也会删除项目之间的填充。这将产生 OP 想要解决的问题..
              【解决方案12】:
              <ListView
                  android:clipToPadding="false"
                  android:paddingTop="10dp"
                  android:paddingBottom="10dp"
                  android:dividerHeight="10dp"
                  android:divider="@null"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
              </ListView>
              

              并将paddingTop、paddingBottom 和dividerHeight 设置为相同的值,以使列表顶部和底部的所有元素和空间之间的间距相等。

              我将clipToPadding 设置为false,以便在此填充区域中绘制视图。

              我将divider 设置为@null 以删除列表元素之间的行。

              【讨论】:

                【解决方案13】:

                另外一种增加列表项间距的方法是,通过向 layout_height 属性提供所需的间距,向适配器代码添加一个空视图。例如为了增加列表项之间的底部间距,将此虚拟视图(空视图)添加到列表项的末尾。

                <View
                    android:layout_width="match_parent"
                    android:layout_height="15dp"/>
                

                因此,这将在列表视图项之间提供 15 dp 的底部间距。如果父布局是 LinearLayout 并且方向是垂直的,您可以直接添加它,或者对其他布局采取适当的步骤。希望这会有所帮助:-)

                【讨论】:

                  【解决方案14】:

                  你只需要使列表分隔符的背景透明,并根据你需要的间隙制作高度。

                  <ListView 
                           android:id="@+id/custom_list"
                           android:layout_height="match_parent"
                           android:layout_width="match_parent"
                           android:divider="#00ffffff"
                           android:dividerHeight="20dp"/>
                  

                  【讨论】:

                    【解决方案15】:

                    如果您使用的是 Horizo​​ntalListView,我发现了一个不太好的解决方案,因为分隔线似乎不适用于它,但我认为它适用于更常见的 ListView。

                    只是添加:

                    <View
                    android:layout_marginBottom="xx dp/sp"/>
                    

                    在您在适配器类中膨胀的布局的最底部视图中,将在项目之间创建间距

                    【讨论】:

                      【解决方案16】:

                      为了在 listView 中的视图之间留出间距,请在膨胀视图上使用填充。

                      您可以在您的视图上使用android:paddingBottom="(number)dp" && android:paddingTop="(number)dp" 或在列表视图中膨胀的视图。

                      分隔线解决方案只是一种解决方法,因为有一天,当您想要使用分隔线颜色(现在它是透明的)时,您会看到分隔线被拉伸了。

                      【讨论】:

                        【解决方案17】:

                        很多这些解决方案都有效。但是,如果您想要的只是能够设置项目之间的边距,我想出的最简单的方法是将您的项目(在您的情况下为 CheckedTextView)包装在 LinearLayout 中,并将项目的边距格式放入其中,不是根布局。一定要给这个包装布局一个 id 并在你的适配器中与你的 CheckedTextView 一起创建它。

                        就是这样。实际上,您正在为 ListView 实例化项目级别的边距。因为 ListView 不知道任何项目布局 - 只有您的适配器知道。这基本上夸大了之前被忽略的项目布局部分。

                        【讨论】:

                          【解决方案18】:

                          这将帮助您添加分隔线高度。

                           getListView().setDividerHeight(10)
                          

                          如果要添加自定义视图,可以在 listView 项目布局本身中添加一个小视图。

                          【讨论】:

                            【解决方案19】:

                            也许你可以尝试在最外层的Layout中添加android:layout_marginTop = "15dp"和android:layout_marginBottom = "15dp"

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 1970-01-01
                              • 2019-05-21
                              • 1970-01-01
                              • 2022-01-18
                              • 2017-01-27
                              • 1970-01-01
                              • 2014-08-13
                              • 1970-01-01
                              相关资源
                              最近更新 更多