【问题标题】:android navigation menu covers actionbarandroid导航菜单覆盖actionbar
【发布时间】:2018-07-03 21:51:53
【问题描述】:

我有一个 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" />

这就是应用的样子

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

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

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

但是没用

【问题讨论】:

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


【解决方案1】:

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

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>

【讨论】:

    【解决方案2】:

    您应该将 DrawerLayout 移动为顶级父级,并将 Toolbar 移出 DrawerLayout 内容容器。简而言之,这看起来像:

    • 相对布局
    • ---------工具栏
    • ------------抽屉布局
    • --------------内容视图
    • -------抽屉列表。

      <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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多