【问题标题】:Android list view wrap content until a certain size based on footer sizeAndroid列表视图将内容包装到基于页脚大小的特定大小
【发布时间】:2013-09-28 17:47:12
【问题描述】:

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

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

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

列表视图应占用与其拥有的项目一样多的空间。其余的由receiptSection 承担。

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

列表视图与 2 个项目一样大,其余由收据视图占用。

如果我添加另一个项目,列表视图现在会占用更多空间并将收据视图向下推:

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

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

我的尝试

我创建了一个列表视图。我有android:layout_alignParentTop="true"android:layout_height="wrap_content"

这将使它随着它的内容和它在视图的顶部一起增长

<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。

对于这个视图,我有 android:layout_alignParentBottom="true"android:layout_below="@id/listViewProducts" 这将使它进入列表视图并与视图底部对齐。

我还使用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 正确占用剩余空间。

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

当receiptView达到50dp时,有没有办法让listView停止增长?

非常感谢您的帮助。

【问题讨论】:

    标签: java android android-layout android-listview height


    【解决方案1】:

    尝试交换“layout_below”。

    您实际上是在说以下内容:请将我的 relativelayout 放在列表视图的下方。如果您希望您的列表视图尊重相对布局的高度,您必须在列表视图中说:

     <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>
    

    【讨论】:

    • 这对我不起作用。 “页脚”部分只占据了整个屏幕,因为它的高度设置为 match_parent。如果将其设置为 wrap_content,即使列表视图只有 1 个项目,它也会一直停留在屏幕底部,这是我们要解决的问题。
    【解决方案2】:

    不幸的是,我认为最好的办法是创建一个扩展 ListView 并覆盖 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多