0

已经为此苦苦挣扎了一段时间。我有一个由各种 LinearLayouts 组成的布局 XML,具有单独的权重。最终结果看起来像这样..

主活动屏幕的图像

不要介意颜色,它只是为了查看断点。无论如何,在终端/原点等 LinearLayout 下方是一个使用自定义适配器填充的 ListView。数据加载正常,但随后 listView “突破”了 LinearLayout,并占据了大部分页面

在此处输入图像描述

(不要介意颜色,它只是为了查看断点..)

IE。它流过上面和下面的视图。

我的 XML 如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="HardcodedText" >

<RelativeLayout
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:background="#FF0000"
    android:gravity="right" >

    <Button
        android:id="@+id/update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:onClick="getFlightInfo"
        android:text="Refresh" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/table_header"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="0.4"
    android:background="#FFFFFF"
    android:weightSum="1" >

    <TextView
        android:id="@+id/header_cell1"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="@string/cell_1_weight"
        android:gravity="center_vertical"
        android:paddingLeft="5dp"
        android:paddingRight="12dp"
        android:text="@string/table_header_terminal"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/header_cell2"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="@string/cell_2_weight"
        android:gravity="center_vertical"
        android:paddingRight="12dp"
        android:text="@string/table_header_origin"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/header_cell3"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="@string/cell_3_weight"
        android:gravity="center_vertical"
        android:paddingRight="12dp"
        android:text="@string/table_header_flight"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/header_cell4"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="@string/cell_4_weight"
        android:gravity="center_vertical"
        android:paddingRight="12dp"
        android:text="@string/table_header_scheduled"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/header_cell5"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="@string/cell_5_weight"
        android:gravity="center_vertical"
        android:paddingRight="12dp"
        android:text="@string/table_header_status"
        android:textSize="11dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/table_list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

<LinearLayout
    android:id="@+id/filter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#00FF00" />

</LinearLayout>

我可能错过了一些明显的东西,但对于我的生活,我看不到它。任何人都可以帮忙吗?

太感谢了..

4

4 回答 4

2

这是因为您保留了 LinearLayout 的高度(一个包装列表视图)“wrap_content”。将其大小固定到某个下垂可以解决您的问题。

然后将列表视图的高度设为“fill_parent”。

编辑 :

试试这种方式:

...

<ListView
    android:id="@+id/list_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</ListView>

在上面的两个布局中,您使用 0.2 和 0.4 作为权重,但在下面的两个布局中,您的用户 3 和 1 ...尝试以前的方式,看看是否可以帮助您。也尝试制作 LinearLayout 的高度(一个包装列表视图) 到 0dip。

于 2012-07-26T10:38:00.767 回答
2

也许已经晚了,但我可以帮助别人。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical">

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="Content 1" />

</LinearLayout>


<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#00ff00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="Content 2" />

</LinearLayout>

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/linearLayout2"
    android:layout_below="@id/linearLayout1"
    android:background="#00f0f0"></ListView>

结果: 结果

于 2016-04-16T21:09:25.203 回答
0

好的..在查阅了这篇文章后,我想出了一个修复方法

  1. 去掉ListView外的LinearLayout,是多余的。
  2. 将高度 '0px'(我在网上找到)添加到 ListView 以确保它正确遵守 android:weight 参数。

希望这可以帮助。

于 2012-07-26T10:43:13.637 回答
0
  • 去掉 的重量id/filter
  • ListView权重为 1 和fill_parent高度。
  • 删除 listView 的直接父级。

您错过的另一件事是weightSum=sum_of all_child_weight在您的根目录中设置。那应该可以解决问题。

于 2012-07-26T10:43:33.200 回答