在 android 中的操作栏上设置导航抽屉

set navigation drawer on actionbar in android

我创建了一个导航 drawer.I 想在状态栏下方显示抽屉。它工作正常,就像我一样 expected.But 在版本 5 和 above.I 想要将抽屉设置为第二个 image.How 以在所有版本的状态栏下方的操作(即)上设置导航抽屉。`

<?xml version="1.0" encoding="utf-8"?>
<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="close"
   >

    <include
        layout="@layout/emp_app_bar_home"
        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="fill_parent"
        android:layout_gravity="start"

        android:fitsSystemWindows="true"

        app:itemTextColor="@color/black"
        app:itemIconTint="@color/black"
        android:background="@drawable/menubg"
        app:headerLayout="@layout/emp_header"
        app:menu="@menu/activity_emp_drawer" />


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

`

尝试将 android:fitsSystemWindows="true" 删除到您的 DrawerLayout 或将其设为假 android:fitsSystemWindows="false",这可能对您有用。

将您的代码替换为:

<?xml version="1.0" encoding="utf-8"?>
<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">

<include
    layout="@layout/emp_app_bar_home"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:itemTextColor="@color/black"
    app:itemIconTint="@color/black"
    android:background="@drawable/menubg"
    app:headerLayout="@layout/emp_header"
    app:menu="@menu/activity_emp_drawer" />

我所做的是找到状态栏的高度并对其进行偏移。

private NavigationView mNavView;
private View mHeaderView;
//...
mNavView = (NavigationView) findViewById(R.id.navView);
//...
mHeaderView = mNavView.getHeaderView(0);
//...
LinearLayout mHeaderLayout = (LinearLayout) HeaderView.findViewById(R.id.headerLayout);
//..
mHeaderLayout.setPadding(0, getStatusBarHeight(), 0, 0);

获取状态栏高度的函数为:

public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
}

希望对您有所帮助。

Ps。查找状态栏高度我很久以前在别处找到的...

android:fitsSystemWindows="true" 放入您在 app:headerLayout="@layout/emp_header"

中提到的 emp_header