38

Ich habe ein Layoutproblem beim Kombinieren von CoordinatorLayout mit einem AppBarLayout und einem NavigationDrawer.CoordinatorLayout + AppBarLayout + NavigationDrawer

Das Problem ist, dass der NavigationDrawer und sein Inhalt hinter der Symbolleiste versteckt sind. Ich habe schon viel recherchiert und viel umstrukturiert, aber keine der "Lösungen" hat mein Problem gelöst.

Eine Demonstration kann in diesem kleinen Webm Video zu finden: https://goo.gl/yWj3VM

Der Basis-Stil ist Theme.AppCompat.Light.NoActionBar.

Mein activity_overview.xml sieht wie folgt aus:

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

Antwort

71

Ihre CoordinatorLayout Ihre DrawerLayout und NavigationView wickelt, die den Koordinator Mittel in der Hand, wie alles angelegt. Sie müssen den Coordinator in der Schublade verschachteln, wie folgt:

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

Das sollte es aussortieren!

+1

für mich gearbeitet Dank – sector11

+0

Aber das bricht die Snackbar UI, wenn Sie versuchen, so etwas hinzuzufügen ... – slott

+0

@slott Wie zeigen Sie ein? Ich habe mich nicht mit ihnen herumgeschlagen, aber dieser Link scheint darauf hinzuweisen, dass Sie eine ID an den Koordinator anhängen und diese dann als Ansicht weitergeben müssen, wenn Sie die Snackbar machen: https://developer.android .com/reference/android/support/design/widget/Snackbar.html # machen (android.view.View, int, int) – GeordieMatt

6

Um der vorherigen Antwort hinzuzufügen, können Sie den DrawerLayout sauberer machen, indem Sie die CoordinatorLayout + untergeordneten Elemente in eine separate XML-Datei verschieben. Und dann benutzen Sie einfach die "include" -Option, um die Datei hinzuzufügen.

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

Guten Ruf. Meistens verwende ich die Schublade als Layout für eine Aktivität und lade dann ein Fragment in FrameLayout, wo du dein Include hast. Bedeutet, dass ich die Aktivität verwenden kann, um alle meine schubladenbasierte Navigation zu verwalten. – GeordieMatt