【问题标题】:How to get the latest Android Navigation Drawer to open below the actionbar/toolbar?如何在操作栏/工具栏下方打开最新的 Android 导航抽屉?
【发布时间】:2015-11-02 15:52:09
【问题描述】:

我已经查看了有关此问题的其他问题和答案,但自从这些问题以来,导航抽屉活动布局和代码略有变化。现在我有这个:

但我希望抽屉在类似于此的操作栏/工具栏下方打开:

activity_main.xml 已编辑:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /> <changed to wrap_content

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

<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="false"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />


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

</LinearLayout>

更改为 wrap_content 后,我​​得到了这个:

app_bar_main.xml:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
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:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

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

我认为 activity_main.xml 和/或 app_bar_main.xml 中的某些内容需要更改...我可能是错的。有什么想法吗?

【问题讨论】:

标签: android navigation-drawer


【解决方案1】:

您只需重新构建 xml 布局,将 DrawerLayout 放在父视图内,而将 Toolbar 放在外边。像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" /> <-- **Change this to wrap_content**

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

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

【讨论】:

  • 这是有道理的@vguzzi。稍后我会尝试并更新。谢谢
  • 试过@vguzzi,但发生的只是切换菜单动画。看不到抽奖打开或关闭。一定是一些简单但不太明白的事情
  • 所以它没有用修改后的xml打开,但它之前打开过?
  • 没错.. 就好像抽屉被隐藏了一样。在 xml 的设计元素中,如果有意义的话,它会显示抽屉布局被挤压到底部
  • 啊...这是因为您将 app_bar_main 设置为 match_parent。将其更改为 wrap_content。
猜你喜欢
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 2015-02-26
相关资源
最近更新 更多