【问题标题】:How to make the nav-drawer appear below the action bar如何使导航抽屉出现在操作栏下方
【发布时间】:2018-03-31 21:05:56
【问题描述】:

我一直在努力让我的导航抽屉出现在操作栏下方。我知道这可能不符合设计指南。 我访问了有关该主题的其他几个线程(例如:Navigation drawer below Actionbar),但没有解决我的问题。

经过大量的修补,我没有走得更远。请帮忙解决这个问题。


目前情况: https://i.stack.imgur.com/HK1qW.png

布局/activity_spellbook.xml

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <include
            layout="@layout/app_bar_spellbook"
            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="left|start"
            android:fitsSystemWindows="false"
            app:headerLayout="@layout/nav_header_spellbook"
            app:menu="@menu/activity_spellbook_drawer" />

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

【问题讨论】:

    标签: android layout android-actionbar navbar


    【解决方案1】:

    只需将应用栏放在DrawerLayout之外。

    代码:

    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:fitsSystemWindows="false"
            app:headerLayout="@layout/nav_header_spellbook"
            app:menu="@menu/activity_spellbook_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
        <include
            layout="@layout/app_bar_spellbook"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    

    【讨论】:

    • 不幸的是,奇怪的是,这会导致导航栏根本不出现。
    • @Roeland 我的错,将应用栏放在“DrawerLayout”下。我编辑了我的答案。
    • 我已经很感谢你的帮助了,我不想问太多,但我遇到了一个新问题:imgur.com/a/lAh3c 菜单是透明的,尽管我试图再次修复它。我是初学者,这无济于事:|
    • @Roeland 如果我回答了您的问题,请将我的回答标记为答案,以便人们也可以查看和学习。关于你的下一个问题,试着给你的 NavigationView 一个背景 'android:background="#ffffff" '。不用担心,我很乐意提供帮助。
    • 我希望能尽快标记它。显然,背景颜色不是问题。不过我会继续努力。我认为这至少是朝着正确方向迈出的一步。
    【解决方案2】:

    您必须对下面给出的所有文件进行一些更改:

    抽屉布局:

    <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="false"
    tools:openDrawer="start">
    
       <include
        layout="@layout/app_bar_main2"
        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:layout_marginTop="?attr/actionBarSize"
        app:headerLayout="@layout/nav_header_main2"
        app:menu="@menu/activity_main2_drawer" />
    
      </android.support.v4.widget.DrawerLayout>
    

    现在,在 AppBarLayout 中,它出现在主类(或您编写的地方)中

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

    现在,最后在清单文件中将主题添加到您的活动中:

    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多