使用ConstraintLayout时,视图与工具栏重叠的问题

19
我遇到了使用ConstraintLayout的兼容工具栏和重叠ListView的问题
我想要做的就是有一个工具栏,然后在其余的空间中有一个ListView。非常简单
这是我的布局
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                         xmlns:app="http://schemas.android.com/apk/res-auto"
                                         android:layout_width="match_parent"
                                         android:layout_height="match_parent">

  <android.support.v7.widget.Toolbar
        android:id="@+id/app_toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Toolbar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:visibility="visible"
        app:layout_constraintTop_toTopOf="parent"
        />

  <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/app_toolbar2"
        />

</android.support.constraint.ConstraintLayout>

我不确定问题是否在于ListView的 layout_height 优先级高于其他所有内容,因此为了确保ListView与父容器一样高,要么会有重叠,要么底部被截断,因为工具栏占用了一些空间。 那么,在工具栏之后使ListView占用剩余的垂直空间的正确方法是什么?

6个回答

26
根据ConstraintLayout的官方文档:
  • 需要使用MATCH_CONSTRAINT(0dp)而不是MATCH_PARENT

重要提示:不建议在ConstraintLayout中包含小部件时使用MATCH_PARENT。可以通过将相应的左/右或上/下约束设置为“parent”并使用MATCH_CONSTRAINT来定义类似的行为。

只需将Listview宽度和高度更改为"0dp"
<ListView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/app_toolbar2"/>

感谢您的解释,我一直在苦苦挣扎约束布局,但一直没有时间阅读文档。给你点赞! - Edgar
将视图高度设置为0dp意味着视图将覆盖其他视图剩余的所有空间,对于这个解决方案点赞! - Prajwal Waingankar

23

试一下这个,应该可以工作:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="56dp"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"/>

<android.support.constraint.ConstraintLayout
    android:id="@+id/main_area"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@+id/toolbar"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent">

    <ListView
        android:id="@+id/listview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

12
这个方法可行,但嵌套的 ConstraintLayout 不必要。诀窍在于将 layout_width 和 height 设为 "0dp" 而不是 match_parent。我不知道这里的 0 有特殊的含义。 - Hilikus
4
很好的回答,我只是认为第二个ConstraintLayout不必要,可以直接在ListView上使用app:layout_constraintTop_toBottomOf="@+id/toolbar"和android:layout_height="0dp"。 - Kevin Ramirez Zavalza
什么都对我没用。我尝试了将0dp放入,但对我仍然没有效果。 - Abhishek Dwarakanath

2

使用以下方法添加工具栏

- <android.support.design.widget.CoordinatorLayout> or ConstraintLayout
    -<android.support.design.widget.AppBarLayout>
       - <android.support.v7.widget.Toolbar>
       - </android.support.v7.widget.Toolbar>
   - </android.support.design.widget.AppBarLayout>

  -<LinearLayout
     app:layout_behavior="@string/appbar_scrolling_view_behavior">
     -<ListView>
     -</Listview>
  -</LinearLayout>
-</android.support.design.widget.CoordinatorLayout> or ConstraintLayout

2
尝试这个...应该能行。 在约束布局中使用以下代码。
<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<FrameLayout
    android:id="@+id/container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar" />

1
app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到您的 ListView 中以设置滚动和间距行为。它适用于 CoordinatorLayout 或 RelativeLayout,可能也适用于 ConstraintLayout。

我已经尝试过了,但它不起作用。一切看起来都完全一样。 - Hilikus

-1

基本上,Framelayout 试图重叠其他视图,这就是为什么会发生这种情况,尝试将 Framelayout 放在其他 Viewgroup 中。

干杯


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接