38

CoordinatorLayoutAppBarLayoutNavigationDrawer合并时,我遇到了布局问题。CoordinatorLayout + AppBarLayout + NavigationDrawer

问题是,NavigationDrawer及其内容隐藏在工具栏后面。我已经做了大量的研究并尝试了很多重组,但是没有一个“解决方案”解决了我的问题。

演示可以在此不多WebM视频中找到:https://goo.gl/yWj3VM

的基本样式为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> 

回答

71

你CoordinatorLayout披着你的DrawerLayout和NavigationView,这意味着协调员在一切是如何布局的控制。您需要将协调器嵌套在抽屉内,如下所示:

<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

感谢 – sector11

+0

为我工作,但这个打破了小吃吧UI,如果你尝试添加这样的事情...... – slott

+0

@slott你是如何显示的?我没有和他们混淆过,但是这个链接似乎表明你需要给协调员添加一个id,然后在制作快餐栏时将其作为视图传入:https://developer.android .com/reference/android/support/design/widget/Snackbar.html#make(android.view.View,int,int) – GeordieMatt

6

要添加到以前的答案,您可以通过将CoordinatorLayout +子元素移动到单独的xml文件中来使DrawerLayout更清洁。然后使用“包含”选项来添加文件。

<?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> 
+0

好喊。大多数情况下,我使用抽屉作为活动的布局,然后将一个片段加载到FrameLayout中,在其中包含您的包含。意味着我可以使用该活动来处理我所有基于抽屉的导航。 – GeordieMatt

相关问题