java - Android ListView 根据页脚大小将内容包装到一定大小

标签 java android android-layout android-listview height

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

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

我在顶部有一个 ListView (listViewProducts),在它下面有另一个相对 View (receiptSection)。

ListView 应该占用与其包含的项目一样多的空间。剩下的由receiptSection拿走。

例如,如果我在 listViewProducts 中有 2 个项目:

first image

ListView 与 2 个项目一样大,其余部分由 receiptView 占用。

如果我添加另一个项目, ListView 现在会占用更多空间并将 receiptView 推得更低:

enter image description here

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

enter image description here

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

我尝试过的

我创建了一个 ListView 。我有 android:layout_alignParentTop="true"android:layout_height="wrap_content"

这将使它随内容一起增长并位于 View 的顶部

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

然后我创建了一个 RelativeLayout,它将保存在单独的 xml 布局文件中的 checkout_receipt_view。

对于这个 View ,我有 android:layout_alignParentBottom="true"android:layout_below="@id/listViewProducts" 可以让它进入 ListView 下,与 View 底部对齐。

我还使用了 android:minHeight="50d" 来设置 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 随项目增长,receiptView 正确占用剩余空间。

问题 但是最小高度不起作用。 ListView 继续无限增长,receiptSection 将被推出 View 。

有什么方法可以让 listView 在 receiptView 达到 50dp 时停止增长?

非常感谢您的帮助。

最佳答案

不幸的是,我认为最好的办法是创建一个扩展 ListView 并覆盖 onMeasure 的自定义 View 。

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);
    }
}

关于java - Android ListView 根据页脚大小将内容包装到一定大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18965887/

相关文章:

java - Android - java - 计算字数

android - Facebook,使用 requestCode=32665 调用 onActivityResult()

java - 为什么 editText 中的文本没有水平居中?

java - 一个线程可以访问同步非静态方法,而另一个线程可以同时访问同步静态方法吗?

java - Java 未处理的异常类型

android - 如何捕捉动画 View 作为视频

android - 如何在android中修改来电和去电屏幕 View

android - 支持多屏幕 - 支持低分辨率的平板电脑

java - Worker 上的数据库更改后 LiveData 未更新

java - 在本地计算机和生产环境中获取不同的日期格式