38

Ho un problema di layout quando si combina lo CoordinatorLayout con uno AppBarLayout e uno NavigationDrawer.CoordinatorLayout + AppBarLayout + NavigationDrawer

Il problema è che NavigationDrawer e il suo contenuto sono nascosti dietro la barra degli strumenti. Ho già fatto molte ricerche e provato molte ristrutturazioni, ma nessuna delle "soluzioni" ha risolto il problema.

Una manifestazione si possono trovare in questo piccolo video Webm: https://goo.gl/yWj3VM

Lo stile di base è Theme.AppCompat.Light.NoActionBar.

mio activity_overview.xml assomiglia a questo:

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

risposta

71

tuo CoordinatorLayout è avvolgente tua DrawerLayout e NavigationView, il che significa che il coordinatore è in controllo di come tutto è disposto. È necessario nidificare il coordinatore all'interno del cassetto, in questo modo:

<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> 

Questo dovrebbe risolvere il problema!

+1

ha funzionato per me, grazie – sector11

+0

Ma questo rompe lo Snackbar interfaccia utente, se si tenta di aggiungere una cosa del genere ... – slott

+0

@slott Come stai mostrando uno? Non ho mai giocato con loro, ma questo link sembra indicare che avresti bisogno di allegare un id al coordinatore e poi passarlo come vista quando fai lo snackbar: https://developer.android .com/riferimento/android/supporto/design/widget/Snackbar.html # make (android.view.View, int, int) – GeordieMatt

6

Per aggiungere alla risposta precedente, è possibile rendere il pulitore DrawerLayout spostando gli elementi figlio CoordinatorLayout + in un file xml separato. E quindi basta usare l'opzione "include" per aggiungere il file.

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

Buon urlo. La maggior parte delle volte, utilizzo il cassetto come layout per un'attività, quindi carica un frammento in FrameLayout in cui è presente l'inclusione. Significa che posso utilizzare l'attività per gestire tutta la mia navigazione basata su cassetti. – GeordieMatt