繁体   English   中英

LinearLayout minHeight 不适用于 weight="1"

[英]LinearLayout minHeight not working with weigth="1"

我整个下午都在尝试让minHeight属性起作用。

我想要的是linearMe布局:

  • 当 ListView 只有几个元素时,从屏幕底部拉伸到 ListView 底部。
  • 例如,我需要能够用图片填充 linearMe 布局。

随着 ListView 变大,我希望linearMe布局:

  • 具有固定高度(在屏幕底部时)和 ListView 以允许滚动。

我的问题是随着 ListView 中有更多元素, linearMe布局越来越小。 而当 listView 有足够的元素填满屏幕时, linearMe布局就消失了。 在这种情况下,看起来minHeight没用。

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" >

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

<LinearLayout
    android:id="@+id/linearMe"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#FF0000"
    android:minHeight="200dp" />

</LinearLayout>

在此处输入图像描述在此处输入图像描述

我希望你能帮助我::)

也将android:layout_weight="1"到 ListView。 将它们(list & linearMe) android:layout_height属性更改为"match_parent"删除`minHeight。

这样每个视图将占据屏幕的一半。`

您可能想使用“View”而不是“LinearLayout”来尝试间隔布局。 ViewGroup 类有时会稍微不同地处理布局。

如果您愿意使用ConstraintLayout作为父容器,以下 xml 应该可以满足您的目的:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constrainedHeight="true"
    app:layout_constraintBottom_toTopOf="@id/linearMe"
    app:layout_constraintTop_toTopOf="parent"
    tools:listitem="@android:layout/list_content" />

<LinearLayout
    android:id="@+id/linearMe"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#FF0000"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@id/list"
    app:layout_constraintHeight_min="200dp"
    android:orientation="vertical"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

我所做的是我已经定义了将listlinearMe相互链接的约束。 app:layout_constraintHeight_min="200dp"将确保 linearMe 的最小高度为linearMe app:layout_constrainedHeight="true"确保当list有更多项目时,它不会隐藏在linearMe后面。

我无法用一些数据来测试它来填充ListView 您可以尝试一下并在此处发布您的反馈。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM