首页 > 解决方案 > 无法使用 DrawerLayout 将图像放在 ListView 上方

问题描述

我想使用约束布局来制作我的应用程序,但我的侧边导航菜单只能与 DrawerLayout 一起使用,所以现在我无法将元素放在我想要的位置。

即使图像设置为“wrap_content”,它也是全屏的并且在我的列表前面,我希望它位于列表上方。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <ListView
        android:id="@+id/lvLista"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/titleDates"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="#B5D5B5"
        app:layout_constraintTop_toBottomOf="@+id/titleDates" />

    <ImageView
        android:id="@+id/titleDates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        app:srcCompat="@drawable/dias_de_coleta" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu" />

</android.support.v4.widget.DrawerLayout>

标签: androidandroid-studiolistviewnavigation-drawerdrawerlayout

解决方案


您必须将 ListView 和 ImageView 放在它自己的 ViewGroup 中,因为 DrawerLayout 不支持任何视图定位约束。您可以将其视为 FrameLayout。

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/titleDates"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf=parent"
            app:srcCompat="@drawable/dias_de_coleta" />
        <ListView
            android:id="@+id/lvLista"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@+id/titleDates"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="#B5D5B5" />       

    </android.support.constraint.ConstraintLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu" />

</android.support.v4.widget.DrawerLayout>

另外: layout_alignParentToplayout_below仅在容器是 RelativeLayout 时才有效。

并且layout_constraintTop_toBottomOf仅在容器是 ConstraintLayout 时才有效。

如果您的视图不是这些视图组的子视图,则使用它们毫无用处。


推荐阅读