使用ConstraintLayout

时间:2018-03-31 03:29:09

标签: android android-constraintlayout

我使用ConstraintLayout发生了一个带有compat工具栏的重叠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 个答案:

答案 0 :(得分:4)

根据ConstraintLayout docs:您需要使用MATCH_CONSTRAINT(0dp)而不是MATCH_PARENT

  

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

只需更改Listview widht&amp; height = “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"/>

答案 1 :(得分:3)

试试这个,应该有效:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

使用以下方法添加for($i=0; $i < count($json); $i++) { $response['url'][$i] = $json[$i]['name']; } echo json_encode($response);

Toolbar

答案 4 :(得分:0)

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

欢呼

答案 5 :(得分:0)

试试这个..应该可行。 在约束布局中使用此代码。

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