【问题标题】:Don't want distance between items of Listactivity不希望 Listactivity 项目之间的距离
【发布时间】:2013-01-02 10:55:51
【问题描述】:

报告布局的我的 xml:

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

    </ListView>

<TextView
    android:id="@+id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/main_no_items"/>

</LinearLayout>

正如您在图片中看到的,列表项之间存在不必要的距离。我尽一切努力使列表项之间的距离最小化,但它永远不会减少:(我希望两个项目之间没有间隙。请指导我如何在一个列表项之后获得下一个项目,除了分区线之外没有间隙。

【问题讨论】:

  • 在您的行中,您有一个包含 4 个 weight=1 文本视图的 wrap_content 垂直线性布局。您可以理解这对布局测量有何影响。删除重量并为您的文本视图使用 wrap_content 高度。
  • 我做了但没有解决:(请给我一个建议
  • 你在所有 4 个文本视图上都这样做了?
  • 是的,我从所有文本视图中删除权重并编辑 layout_height = "wrap_content"。请指导

标签: java android xml android-layout


【解决方案1】:

这个差距是因为您在row layout 中使用的LinearLayout 的背景。该背景图片的高度很长,这就是为什么它显示两个项目之间的差距。尝试让背景图片的高度更小,以达到您的要求。

【讨论】:

  • 我倾向于同意。看来bg很高。
【解决方案2】:

尝试改变

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@drawable/background"
android:orientation="vertical" >

android:layout_height="wrap_content"

【讨论】:

    【解决方案3】:

    永远不要在 raw_layout 中设置背景..

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/background"         <-----  remove this line
    android:orientation="vertical" >
    

    【讨论】:

    • 尚未解决:(请给我建议如何解决??
    【解决方案4】:

    从行的根布局中移除背景并将其应用于listview

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="?android:attr/listPreferredItemHeight"
          android:background="@drawable/background"  <===== Remove background
          android:orientation="vertical" >
    

    报告布局

    中应用它
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         android:background="@drawable/background" /> <=== Try applying background in listview.
    

    【讨论】:

    • 您应用的背景是图像还是选择器?这是您在行布局中应用的图像的问题。我猜。