Android导航菜单涵盖了操作栏

时间:2018-07-03 13:14:38

标签: java android menu android-actionbar navigation-drawer

我有一个xml布局,其中包含一个操作栏,一个导航菜单和一个主要内容。

操作栏包含一个搜索区域和一个用于切换导航菜单的按钮。

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

这是应用程序的外观

enter image description here

当我显示菜单时,它会覆盖我的操作栏

enter image description here

我尝试将其添加到导航抽屉中

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

但这没用

2 个答案:

答案 0 :(得分:0)

您应该将DrawerLayout移到最上层,并将工具栏移出DrawerLayout内容容器。简而言之,它看起来像:

  • RelativeLayout
  • ---------工具栏
  • ----------- 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:     但是,Material Design准则指出Navigation Drawer应该在Toolbar之上。

答案 1 :(得分:0)

我以前也遇到过同样的问题。这是现在的样子:

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>