【问题标题】:Can't put an Image above a ListView using DrawerLayout无法使用 DrawerLayout 将图像放在 ListView 上方
【发布时间】:2018-07-09 19:23:24
【问题描述】:

我想使用约束布局来制作我的应用,但我的侧边导航菜单只能使用 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>

【问题讨论】:

    标签: android android-studio listview navigation-drawer drawerlayout


    【解决方案1】:

    您必须将 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 时才有效。

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

    【讨论】:

    • 成功了,谢谢!不知道我可以在同一个 xml 中使用不同的布局,在那些代码行中我拼命试图让它工作。
    【解决方案2】:
    <android.support.v4.widget.DrawerLayout 
     <android.support.constraint.ConstraintLayout
          <ImageView/>
          <ListView/>
     </android.support.constraint.ConstraintLayout>
     <android.support.design.widget.NavigationView/>
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

    • 问卷希望将列表视图放在图像视图下方,因此在约束布局中放置第一个图像视图,然后在该图像视图下方放置列表视图并解决问题。
    猜你喜欢
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多