【问题标题】:Distance between Listview items having Divider具有 Divider 的 Listview 项目之间的距离
【发布时间】:2012-07-12 04:00:09
【问题描述】:

我在这个主题上有几个关于 SO 的问题,但所有这些问题都与 Listview 项之间的距离有关,几乎所有答案都建议使用 Transparent Divider 作为解决方案。 p>

但就我而言,我已经在两个列表项之间设置了分隔符,那么如何在两个列表项之间应用一些边距/填充/距离?

任何帮助表示赞赏。

Xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listMainPlans"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@drawable/list_divide"
        android:dividerHeight="2dp" >
    </ListView>

</LinearLayout>

【问题讨论】:

  • 取决于您确切想要实现的布局。您可以创建/扩充自定义列表项并在 XML 中添加所需的填充/边距。
  • 这个 xml 看起来不错。关于margin/padding/distance,如果你已经为ListView准备了自定义适配器,你可以在row xml布局中保留padding/margin。
  • 您必须将布局膨胀为子布局。对吗??
  • @PareshMayani - 我尝试在行 xml 布局中提供边距,但这没有得到应用。
  • 列表视图中有什么?我的意思是你要在列表视图行中显示什么??

标签: android android-layout android-listview margin


【解决方案1】:

基于this tutorial,您可以将padding 添加到listview_item_row.xml

使用您的适配器,您可以为 ListView(本教程中的 WeatherAdapter.java)中的每个元素扩展此 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"> // <-----

     <ImageView  />

     <TextView  />

</LinearLayout>

【讨论】:

  • 它通过将填充顶部和底部设置为父相对布局来工作。
  • 还有另一种方法:我可以添加一个固定高度的虚拟文本视图。
  • 添加一个空的TextView 来构建布局并不是最好的工作方式:)
  • 是的,我知道这种虚拟的 textview 方法,但正在寻找实用的解决方案。非常感谢!
  • @Martin android:layout_alignParentTop="true" android:layout_alignParentBottom="true" 在相对布局中比在线性布局中更有效。请尝试使用相对布局
【解决方案2】:

为该相对布局提供静态高度,例如:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_innerbg">

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/item_background"
        android:minHeight="40dp">

    <TextView 
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:textColor="@color/blue"
        android:layout_marginRight="45dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textStyle="bold"/>



    <TextView 
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:layout_centerInParent="true"
         android:layout_marginRight="10dp"/>

    </RelativeLayout>

</RelativeLayout>

你会得到每行项目之间的距离

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 2015-11-24
    相关资源
    最近更新 更多