您可以像这样添加layout_marginTop
,
<android.support.design.widget.NavigationView
android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
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" />
但是抽屉将出现作为顶层到工具栏。
这里有另一种添加它到工具栏下面的不太流畅的方法!!!
可能不是最好的方法,但它可以工作!
最终结果会看起来像这样
![enter image description here](https://istack.dev59.com/hK4mD.webp)
如果你创建一个导航抽屉项目(Navigation Drawer Activity
),它会在创建时在你的layout
文件夹中给你四个XML文件
- app_bar_main
- content_main
- navigatin_main
activity_main
![enter image description here](https://istack.dev59.com/ehJlO.webp)
这些xml如何链接?大多数情况下,会使用include标签
您的Activity与activity_main
相关联
activity_main
包含app_bar_main
和navigation_view
(抽屉)
app_bar_main
默认包含toolbar
和content_main
现在让我们删除activity_main
并将其内容直接设置为app bar main,并将其用作Activity的主布局。
要将抽屉添加到工具栏下方,请将其添加到android.support.design.widget.AppBarLayout
下,因为它包含工具栏并且应该在顶部。
这里是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="none.navhead.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>
//------ taken from activity_main
// content main
<include layout="@layout/content_main" />
// you need this padding
<android.support.v4.widget.DrawerLayout
android:paddingTop="?attr/actionBarSize"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
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>
</android.support.design.widget.CoordinatorLayout>
p.s. 你可以将 app_bar_main.XML 设置为你的活动的 setContentView,尽管有很多方法可以玩耍 ;)