简体   繁体   中英

android navigation menu covers actionbar

I have an xml layout that contains an actionbar, a navigation menu and a main content.

The action bar contains a search area and a button to toggle the navigation menu.

<?xml version="1.0" encoding="utf-8"?>

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

    <android.support.design.widget.CoordinatorLayout 
        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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar

                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:titleTextAppearance="@style/ToolbarTitleTheme"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout


<android.support.design.widget.NavigationView
    android:id="@+id/navigationMenu"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    app:menu="@menu/menu2" />

This is how the app looks like

在此处输入图片说明

When I show the menu it covers my actionbar

在此处输入图片说明

I tried adding this to the navigation drawer

<android.support.design.widget.NavigationView
    .....
    .....
    android:layout_below="@id/toolbar" />

But it didn't work

You should move DrawerLayout as top parent and move Toolbar out of DrawerLayout content container. In short this looks like:

  • RelativeLayout
  • ---------Toolbar
  • -----------DrawerLayout
  • --------------ContentView
  • -------------------DrawerList.

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/top_parent" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".MainActivity"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar"> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background_color" /> <ListView android:id="@+id/drawer" android:layout_width="260dp" android:layout_height="match_parent" android:layout_below="@+id/toolbar" android:layout_gravity="start" android:layout_marginTop="56dp" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout> 

important: However, Material Design guidelines state that Navigation Drawer should be above the Toolbar .

I faced this same problem too before. Here's how it looks now:

MainActivity.xml

<android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/cpb_white"
    android:fitsSystemWindows="true">
...
...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

 <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:background="@android:color/transparent"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme" />

</RelativeLayout>
...
...

<android.support.design.widget.NavigationView
        android:id="@+id/navigationview"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/cpb_white"
        android:fitsSystemWindows="true"
        android:theme="@style/NavigationTheme"
        app:headerLayout="@layout/header_drawer"
        app:menu="@menu/drawer_mainmenu">


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

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

Styles.xml

<style name="HomePageTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#7200d9</item>
        <item name="colorPrimaryDark">#7200d9</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorControlNormal">#fff</item>
        <item name="colorControlActivated">#d810a7</item>
        <item name="colorControlHighlight">#f44336</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM