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

相關問題