Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android列表视图根据页脚大小将内容包装到特定大小_Java_Android_Android Layout_Android Listview_Height - Fatal编程技术网

Java Android列表视图根据页脚大小将内容包装到特定大小

Java Android列表视图根据页脚大小将内容包装到特定大小,java,android,android-layout,android-listview,height,Java,Android,Android Layout,Android Listview,Height,我正在开发一个android应用程序 在XML布局中,我需要执行以下操作: 我在顶部有一个列表视图(listViewProducts),在它下面有另一个相对视图(receiptSection) 列表视图所占空间应与其包含的项目相同。其余部分由接收部分完成 <RelativeLayout android:id="@+id/receiptSection" android:layout_width="match_parent" android:layout_height=

我正在开发一个android应用程序

在XML布局中,我需要执行以下操作:

我在顶部有一个列表视图(listViewProducts),在它下面有另一个相对视图(receiptSection)

列表视图所占空间应与其包含的项目相同。其余部分由接收部分完成

<RelativeLayout
    android:id="@+id/receiptSection"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/listViewProducts"
    android:minHeight="50dp" >
    <include layout="@layout/checkout_receipt_view" />
</RelativeLayout>
例如,如果listViewProducts中有2项:

列表视图与这两个项目一样大,其余部分由receiptView获取

如果我添加另一项,列表视图现在会占用更多空间并将receiptView推低:

但是,如果我添加了更多的项目,我希望列表视图的高度停止增长,以便为receiptView留下一个不能变小的最小高度:

如图所示,receiptVIew的最小高度为50dp。一旦收据视图达到该高度,它应该停止收缩,现在列表视图根据剩余空间具有固定大小。其余的将是可滚动的

我尝试过的

我创建了一个列表视图。我有
android:layout\u alignParentTop=“true”
android:layout\u height=“wrap\u content”

这将使其随着内容的增长而增长,并使其位于视图的顶部

<ListView
    android:id="@+id/listViewProducts"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" >
</ListView>
listViewProducts随着项目的增加而增长,receiptView正确地占用了剩余的空间

问题 然而,最低高度不起作用。列表视图继续无限增长,receiptSection将被挤出视图

当receiptView达到50dp时,是否有办法使listView停止增长


非常感谢您的帮助。

尝试交换“下面的布局”

你实际上说的是:请把我的relativelayout放在listview下面。如果希望listview尊重relativelayout的高度,则必须在listview中说明:

 <ListView
    android:id="@+id/listViewProducts"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/receiptSection"
    android:layout_alignParentTop="true" >
</ListView>

你的亲戚呢

<RelativeLayout
    android:id="@+id/receiptSection"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:minHeight="50dp" >
    <include layout="@layout/checkout_receipt_view" />
</RelativeLayout>

不幸的是,我认为最好的办法是创建一个自定义视图,扩展
列表视图
并覆盖
onMeasure

public class CustomView extends ListView {

    @Override
    int maxHeight = 0;
    View parentView = (RelativeLayout) (or whatever) getParent();
    if (parentView != null){
        maxHeight = parentView.getHeight() - 50;
    }
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

这对我不起作用。“页脚”部分占据了整个屏幕,因为它的高度设置为与父项匹配。如果将其设置为wrap_content,则即使列表视图只有一个项目,它也会一直保持在屏幕底部,这就是我们试图解决的问题。