协调布局 + 应用栏布局 + 导航抽屉

69

当我将CoordinatorLayoutAppBarLayoutNavigationDrawer组合时,我遇到了布局问题。

问题在于导航抽屉及其内容被工具栏遮挡。我已经进行了大量研究并尝试了许多重构,但没有一个“解决方案”可以解决我的问题。

您可以在这个小的Webm视频中看到演示:https://www.dropbox.com/s/i5zfc2x2ts2fws7/navigation_drawer_stackoverflow32523188.webm?dl=0

基本样式是Theme.AppCompat.Light.NoActionBar

我的activity_overview.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/overview_coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimaryDark"
            app:layout_scrollFlags="enterAlways|scroll" />


    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusableInTouchMode="true">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/lorem_ipsum_long" />
        </android.support.v4.widget.NestedScrollView>

        <android.support.design.widget.NavigationView
            android:id="@+id/nvView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/navigationdrawer_main" />

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


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/overview_floating_action_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"

        android:clickable="true"
        android:src="@drawable/ic_add"
        app:layout_anchor="@id/overview_coordinator_layout"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_behavior="@string/fab_onscroll_behavior" />

</android.support.design.widget.CoordinatorLayout>
3个回答

111

您的CoordinatorLayout包裹了DrawerLayout和NavigationView,这意味着协调者控制所有内容的布局。您需要将Coordinator嵌套在抽屉内,像这样:

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

    <android.support.design.widget.CoordinatorLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/overview_coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimaryDark"
                app:layout_scrollFlags="enterAlways|scroll" />

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/lorem_ipsum_long" />

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

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/overview_floating_action_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:clickable="true"
            android:src="@drawable/ic_add"
            app:layout_anchor="@id/overview_coordinator_layout"
            app:layout_anchorGravity="bottom|right|end"
            app:layout_behavior="@string/fab_onscroll_behavior" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/navigationdrawer_main" />

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

那应该解决了!


1
@slott 你是怎么展示Snackbar的?我还没有尝试过,但是这个链接似乎表明你需要给协调器附加一个id,然后在创建Snackbar时将其作为视图传递进去:https://developer.android.com/reference/android/support/design/widget/Snackbar.html#make(android.view.View, int, int) - GeordieMatt
@GeordieMatt 为什么你在 DrawerLayout 中添加了 android:clickable="true" - Mehdi Dehghani
@MehdiDehghani 我只是重新排列了原始的xml,所以你需要问原始的发布者 :) - GeordieMatt

15

补充前面的答案,你可以通过将CoordinatorLayout和其子元素移动到单独的xml文件中来使DrawerLayout更加简洁。然后只需使用"include"选项添加该文件。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

  <!-- Widget Coordinator + child elements go here -->
  <include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

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

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

2
不错的想法。大多数情况下,我使用抽屉作为活动的布局,然后只需将片段加载到您包含的FrameLayout中即可。这意味着我可以使用活动来处理所有基于抽屉的导航。 - GeordieMatt
2
这也是Google的示例设计方式。如果以这种方式布局,它将易于阅读和维护。 - The_Martian

0
根据《导航抽屉文档》,您可以使用带有顶部应用栏的导航抽屉,就像这段代码一样:
在布局中:
<androidx.drawerlayout.widget.DrawerLayout
...
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.MaterialComponents.AppBarLayout.PrimarySurface"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topAppBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="@string/top_app_bar"app:navigationIcon="@drawable/ic_menu_24dp"
            style="@style/Widget.MaterialComponents.Toolbar.PrimarySurface"
            android:background="@android:color/transparent"
            android:elevation="0dp" />

    </com.google.android.material.appbar.AppBarLayout>

    <!-- Screen content -->
    <!-- Use app:layout_behavior="@string/appbar_scrolling_view_behavior" to fit below top app bar -->

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.navigation.NavigationView
    ...
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start" />

</androidx.drawerlayout.widget.DrawerLayout>

在res/values/themes.xml文件中:
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.*">
<item name="android:windowTranslucentStatus">true</item>

在res/layout/header_navigation_drawer.xml文件中:
<LinearLayout
...
android:fitsSystemWindows="true">

...

在代码中:
topAppBar.setNavigationOnClickListener {
    drawerLayout.open()
}

navigationView.setNavigationItemSelectedListener { menuItem ->
    // Handle menu item selected
    menuItem.isChecked = true
    drawerLayout.close()
    true
}

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