我正在开发一个 android 应用程序。
在 XML 布局中,我需要执行以下操作:
我在顶部有一个 ListView (listViewProducts),在它下面有另一个相对 View (receiptSection)。
ListView 应该占用与其包含的项目一样多的空间。剩下的由receiptSection拿走。
例如,如果我在 listViewProducts 中有 2 个项目:
ListView 与 2 个项目一样大,其余部分由 receiptView 占用。
如果我添加另一个项目, ListView 现在会占用更多空间并将 receiptView 推得更低:
但是,如果我添加更多项目,我希望 ListView 高度停止增长,以便为 receiptView 留下一个不能变小的最小高度:
如图所示,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/