我刚开始在Android Studio中使用预配置的NavigationBar启动了一个项目。很明显,它会将导航抽屉放在操作栏后面。许多相关问题希望将导航抽屉放在操作栏上方,而我想让它从操作栏下方开始。这是我当前的情况:
最终期望的情况如下:
我已经找到了这个解决方案,但我认为应该有更简单的方法。
我刚开始在Android Studio中使用预配置的NavigationBar启动了一个项目。很明显,它会将导航抽屉放在操作栏后面。许多相关问题希望将导航抽屉放在操作栏上方,而我想让它从操作栏下方开始。这是我当前的情况:
最终期望的情况如下:
我已经找到了这个解决方案,但我认为应该有更简单的方法。
在您的根ViewGroup上应用此属性 android:layout_marginTop="?android:attr/actionBarSize"
。希望这可以帮到您。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/app_bar"
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>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Put your content View here!" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
除了Biu的回答之外
还要在您的根视图组中添加此行,以考虑状态栏/导航栏
android:fitsSystemWindows="true"
在这里留下评论,以防有人再次偶然发现这个线程
<item name="android:windowActionBarOverlay">false</item>
或更改主题来解决你的问题。 - Ultimo_m