在android的actionbar上设置导航抽屉

在android的actionbar上设置导航抽屉,android,android-actionbar,navigation-drawer,Android,Android Actionbar,Navigation Drawer,我已经创建了一个导航抽屉。我想在状态栏下方显示该抽屉。它的工作原理与我预期的一样。但是,在版本5及以上的版本中,该抽屉在状态栏上方打开。我想将该抽屉设置为与第二个图像相同。如何为所有版本在状态栏下方的操作(即)上设置导航抽屉` <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/re

我已经创建了一个导航抽屉。我想在状态栏下方显示该抽屉。它的工作原理与我预期的一样。但是,在版本5及以上的版本中,该抽屉在状态栏上方打开。我想将该抽屉设置为与第二个图像相同。如何为所有版本在状态栏下方的操作(即)上设置导航抽屉`

<?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:fitsystemwindows=“true”
到您的
DrawerLayout
或将其设置为类似于
android:fitsystemwindows=“false”
,这可能对您有用。

尝试删除
android:fitsystemwindows=“true”
到您的
抽屉布局
或将其设置为false,如
android:fitsystemwindows=“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" />


用以下代码替换您的代码:

<?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;
}
希望有帮助


注:查找状态栏的高度我很久以前在其他地方找到的…

我所做的是查找状态栏的高度并将其偏移

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;
}
希望有帮助



另外,查找状态栏的高度我很久以前就在别的地方找到了…

安卓:FitsystemWindows=“true”
放到你在
应用程序:headerLayout=“@layout/emp_header”

中提到的
emp_头上
到您在
应用程序:headerLayout=“@layout/emp\u header”

中提到的
emp\u头
,这是您想要的吗?不,不是这样,我需要打开状态栏下方的抽屉。您的示例,如actionbarah下方的抽屉打开,让我看看,这应该可以完成任务-->您知道答案这是您想要的吗?不,不是这样,我需要打开状态栏下方的抽屉。您的示例(如抽屉在操作栏下方打开)Barah找到了,让我看看这应该可以完成任务-->您知道答案吗?您的答案很有效,但有一件事是,如果我为标题布局设置了填充,则意味着某些文本在内部移动。因此,我为mNavView设置了填充。之后正如我所期望的那样,它可以正常工作,但是灰色显示在状态栏的顶部。是否有可能在mNavView中动态设置边距而不是填充边距?我认为如果移动边距,它将显示您看到的灰色背景。所以我认为填充比边距好。顺便问一下,你说一些文本被移到里面是什么意思?如果你仔细看,我改变了布局,而不是标题本身,也不是导航视图。我想你可以尝试将所有内容包装在一个版面中,然后在该版面顶部填充。查看包含用户名和电子邮件id的发布图像。该电子邮件id在版面中移动I你的答案很好,但有一件事是,如果我为headerlayout设置了填充,则表示部分文本在版面中移动。因此,我为mNavView设置了填充。之后正如我所期望的那样,它可以正常工作,但是灰色显示在状态栏的顶部。是否有可能在mNavView中动态设置边距而不是填充边距?我认为如果移动边距,它将显示您看到的灰色背景。所以我认为填充比边距好。顺便问一下,你说一些文本被移到里面是什么意思?如果你仔细看,我改变了布局,而不是标题本身,也不是导航视图。我认为你可以尝试将所有内容包装在一个版面中,然后在该版面顶部填充。查看包含用户名和电子邮件id的发布图像。该电子邮件id在版面中移动I