Android 如何使用抽屉布局在操作栏/工具栏上和状态栏下显示?

Android 如何使用抽屉布局在操作栏/工具栏上和状态栏下显示?,android,navigation-drawer,toolbar,android-appcompat,statusbar,Android,Navigation Drawer,Toolbar,Android Appcompat,Statusbar,我在新的材质设计中看到,您可以将抽屉显示在操作栏上方和状态栏后面。我如何实现这一点?框架中的新功能和对libs的支持正好允许这一点。有三个“拼图块”: 使用,以便可以将操作栏嵌入到视图层次结构中 使安装系统窗口,以便将其放置在系统栏后面 禁用主题.Material的正常状态栏颜色,以便抽屉布局可以在那里绘制 我假设您将使用新的appcompat 首先,您的布局应如下所示: <!-- The important thing to note here is the added fitSyste

我在新的材质设计中看到,您可以将抽屉显示在操作栏上方和状态栏后面。我如何实现这一点?

框架中的新功能和对libs的支持正好允许这一点。有三个“拼图块”:

  • 使用,以便可以将操作栏嵌入到视图层次结构中
  • 使
    安装系统窗口
    ,以便将其放置在系统栏后面
  • 禁用
    主题.Material
    的正常状态栏颜色,以便抽屉布局可以在那里绘制
  • 我假设您将使用新的appcompat

    首先,您的布局应如下所示:

    <!-- The important thing to note here is the added fitSystemWindows -->
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <!-- Your normal content view -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <!-- We use a Toolbar so that our drawer can be displayed
                 in front of the action bar -->
            <android.support.v7.widget.Toolbar  
                android:id="@+id/my_awesome_toolbar"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:minHeight="?attr/actionBarSize"
                android:background="?attr/colorPrimary" />
    
            <!-- The rest of your content view -->
    
        </LinearLayout>
    
        <!-- Your drawer view. This can be any view, LinearLayout
             is just an example. As we have set fitSystemWindows=true
             this will be displayed under the status bar. -->
        <LinearLayout
            android:layout_width="304dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:fitsSystemWindows="true">
    
            <!-- Your drawer content -->
    
        </LinearLayout>
    
    </android.support.v4.widget.DrawerLayout>
    
    然后,您需要确保抽屉布局在状态栏后面可见。您可以通过更改values-v21主题来实现这一点:

    values-v21/themes.xml

    <style name="Theme.MyApp" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
    
    <declare-styleable name="ScrimInsetsView">
        <attr name="insetForeground" format="reference|color" />
    </declare-styleable>
    
    <android.support.v4.widget.DrawerLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">
    
        <!-- The main content view -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <!-- Your main content -->
    
        </LinearLayout>
    
        <!-- The navigation drawer -->
        <com.example.app.util.ScrimInsetsFrameLayout 
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/scrimInsetsFrameLayout"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:elevation="10dp"
            android:fitsSystemWindows="true"
            app:insetForeground="#4000">
    
            <!-- Your drawer content -->
    
        </com.example.app.util.ScrimInsetsFrameLayout>
    
    </android.support.v4.widget.DrawerLayout>
    
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
    

    在values-v21样式或主题xml中,使其正常工作需要使用此属性:

    true


    这就是魔法

    这是最简单的,对我来说很有效:

    在价值观-21中:

    <resources>
        <style name="AppTheme" parent="AppTheme.Base">
            ...
            <item name="android:windowTranslucentStatus">true</item>
        </style>
        <dimen name="topMargin">25dp</dimen>
    </resources>
    
    编辑:新的设计支持库支持此功能,不再需要以前的方法。 现在可以使用新的

    您可以看到Chris Banes的演示版,其中演示了所有新功能


    以前的方法: 由于没有发布完整的解决方案,以下是我实现预期结果的方法

    首先,在项目中包含一个

    /*
    * Copyright 2014 Google Inc.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *     http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
    
    /**
    * A layout that draws something in the insets passed to 
    * {@link #fitSystemWindows(Rect)}, i.e. the area above UI chrome
    * (status and navigation bars, overlay action bars).
    */
    public class ScrimInsetsFrameLayout extends FrameLayout {
        private Drawable mInsetForeground;
    
        private Rect mInsets;
        private Rect mTempRect = new Rect();
        private OnInsetsCallback mOnInsetsCallback;
    
        public ScrimInsetsFrameLayout(Context context) {
            super(context);
            init(context, null, 0);
        }
    
        public ScrimInsetsFrameLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context, attrs, 0);
        }
    
        public ScrimInsetsFrameLayout(
            Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context, attrs, defStyle);
        }
    
        private void init(Context context, AttributeSet attrs, int defStyle) {
            final TypedArray a = context.obtainStyledAttributes(attrs,
                    R.styleable.ScrimInsetsView, defStyle, 0);
            if (a == null) {
                return;
            }
            mInsetForeground = a.getDrawable(
                R.styleable.ScrimInsetsView_insetForeground);
            a.recycle();
    
            setWillNotDraw(true);
        }
    
        @Override
        protected boolean fitSystemWindows(Rect insets) {
            mInsets = new Rect(insets);
            setWillNotDraw(mInsetForeground == null);
            ViewCompat.postInvalidateOnAnimation(this);
            if (mOnInsetsCallback != null) {
                mOnInsetsCallback.onInsetsChanged(insets);
            }
            return true; // consume insets
        }
    
        @Override
        public void draw(Canvas canvas) {
            super.draw(canvas);
    
            int width = getWidth();
            int height = getHeight();
            if (mInsets != null && mInsetForeground != null) {
                int sc = canvas.save();
                canvas.translate(getScrollX(), getScrollY());
    
                // Top
                mTempRect.set(0, 0, width, mInsets.top);
                mInsetForeground.setBounds(mTempRect);
                mInsetForeground.draw(canvas);
    
                // Bottom
                mTempRect.set(0, height - mInsets.bottom, width, height);
                mInsetForeground.setBounds(mTempRect);
                mInsetForeground.draw(canvas);
    
                // Left
                mTempRect.set(
                    0, 
                    mInsets.top, 
                    mInsets.left, 
                    height - mInsets.bottom);
                mInsetForeground.setBounds(mTempRect);
                mInsetForeground.draw(canvas);
    
                // Right
                mTempRect.set(
                    width - mInsets.right, 
                    mInsets.top, width, 
                    height - mInsets.bottom);
                mInsetForeground.setBounds(mTempRect);
                mInsetForeground.draw(canvas);
    
                canvas.restoreToCount(sc);
            }
        }
    
        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
            if (mInsetForeground != null) {
                mInsetForeground.setCallback(this);
            }
        }
    
        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            if (mInsetForeground != null) {
                mInsetForeground.setCallback(null);
            }
        }
    
        /**
         * Allows the calling container to specify a callback for custom 
         * processing when insets change (i.e. when {@link #fitSystemWindows(Rect)}
         * is called. This is useful for setting padding on UI elements 
         * based on UI chrome insets (e.g. a Google Map or a ListView). 
         * When using with ListView or GridView, remember to set
         * clipToPadding to false.
         */
        public void setOnInsetsCallback(OnInsetsCallback onInsetsCallback) {
            mOnInsetsCallback = onInsetsCallback;
        }
    
        public static interface OnInsetsCallback {
            public void onInsetsChanged(Rect insets);
        }
    }
    
    然后创建一个样式表,以便设置
    insetForeground

    值/attrs.xml

    <style name="Theme.MyApp" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
    
    <declare-styleable name="ScrimInsetsView">
        <attr name="insetForeground" format="reference|color" />
    </declare-styleable>
    
    <android.support.v4.widget.DrawerLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">
    
        <!-- The main content view -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <!-- Your main content -->
    
        </LinearLayout>
    
        <!-- The navigation drawer -->
        <com.example.app.util.ScrimInsetsFrameLayout 
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/scrimInsetsFrameLayout"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:elevation="10dp"
            android:fitsSystemWindows="true"
            app:insetForeground="#4000">
    
            <!-- Your drawer content -->
    
        </com.example.app.util.ScrimInsetsFrameLayout>
    
    </android.support.v4.widget.DrawerLayout>
    
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
    
    在活动的onCreate方法中,设置抽屉布局上的状态栏背景色

    MainActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // ...
    
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mDrawerLayout.setStatusBarBackgroundColor(
            getResources().getColor(R.color.primary_dark));
    }
    
    最后更新应用程序的主题,使
    DrawerLayout
    位于状态栏后面

    values-v21/styles.xml

    <style name="Theme.MyApp" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
    
    <declare-styleable name="ScrimInsetsView">
        <attr name="insetForeground" format="reference|color" />
    </declare-styleable>
    
    <android.support.v4.widget.DrawerLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">
    
        <!-- The main content view -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <!-- Your main content -->
    
        </LinearLayout>
    
        <!-- The navigation drawer -->
        <com.example.app.util.ScrimInsetsFrameLayout 
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/scrimInsetsFrameLayout"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:elevation="10dp"
            android:fitsSystemWindows="true"
            app:insetForeground="#4000">
    
            <!-- Your drawer content -->
    
        </com.example.app.util.ScrimInsetsFrameLayout>
    
    </android.support.v4.widget.DrawerLayout>
    
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
    
    
    符合事实的
    @android:彩色/透明
    
    结果:


    以上所有方法都是正确的,可能有效。我已经按照上述指南创建了一个工作演示,并在2.x到5.x上进行了测试

    你可以从中克隆

    重要的事情是在主要活动中

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    res = this.getResources();
    
    this.setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
        ScrimInsetsFrameLayout scrimInsetsFrameLayout = (ScrimInsetsFrameLayout)
                findViewById(R.id.linearLayout);
        scrimInsetsFrameLayout.setOnInsetsCallback(this);
    } 
    
    那回电话呢

    @Override
    public void onInsetsChanged(Rect insets) {
        Toolbar toolbar = this.toolbar;
        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)
            toolbar.getLayoutParams();
        lp.topMargin = insets.top;
        int top = insets.top;
        insets.top += toolbar.getHeight();
        toolbar.setLayoutParams(lp);
        insets.top = top; // revert
    }
    
    V21的主题确实很神奇

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- API 21 theme customizations can go here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/accent_material_light</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
    
    克隆自@Chris Banes

    试试这个:

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true">
    
    
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!--Main layout and ads-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <FrameLayout
                android:id="@+id/ll_main_hero"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
    
            </FrameLayout>
    
            <FrameLayout
                android:id="@+id/ll_ads"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <View
                    android:layout_width="320dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:background="#ff00ff" />
            </FrameLayout>
    
    
        </LinearLayout>
    
        <!--Toolbar-->
        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toolbar"
            android:elevation="4dp" />
    </FrameLayout>
    
    
    <!--left-->
    <ListView
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:background="@mipmap/layer_image"
        android:id="@+id/left_drawer"></ListView>
    
    <!--right-->
    <FrameLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="@mipmap/layer_image">
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@mipmap/ken2"
            android:scaleType="centerCrop" />
    </FrameLayout>
    
    现在,您可以像使用工具栏的普通操作栏一样使用创建选项菜单

    这是我的布局

    • 顶部:左抽屉-右抽屉
      • MID:工具栏(操作栏)
      • 底部:ListFragment

    希望你能理解!玩得开心

    而不是使用
    ScrimInsetsFrameLayout
    。。。添加一个固定高度为
    24dp
    且背景为
    primaryColor
    的视图不是更容易吗

    我知道这涉及到在层次结构中添加一个虚拟视图,但对我来说似乎更清晰

    我已经试过了,效果很好

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_base_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <!-- THIS IS THE VIEW I'M TALKING ABOUT... -->
            <View
                android:layout_width="match_parent"
                android:layout_height="24dp"
                android:background="?attr/colorPrimary" />
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/activity_base_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:elevation="2dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark" />
    
            <FrameLayout
                android:id="@+id/activity_base_content_frame_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </LinearLayout>
    
        <fragment
            android:id="@+id/activity_base_drawer_fragment"
            android:name="com.myapp.drawer.ui.DrawerFragment"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:elevation="4dp"
            tools:layout="@layout/fragment_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
    

    随着最新版本的发布,我们有了一个新的视图,其中一个新视图名为。因此,我们不用使用
    ScrimInsetsFrameLayout
    和所有其他东西,而是使用这个视图,一切都是为我们做的

    实例 第一步 将
    设计支持库
    添加到
    build.gradle
    文件中

    dependencies {
        // Other dependencies like appcompat
        compile 'com.android.support:design:22.2.0'
    }
    
    步骤2 将
    导航视图
    添加到您的
    抽屉布局

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:id="@+id/drawer_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fitsSystemWindows="true"> <!-- this is important -->
    
         <!-- Your contents -->
    
         <android.support.design.widget.NavigationView
             android:id="@+id/navigation"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:layout_gravity="start"
             app:menu="@menu/navigation_items" /> <!-- The items to display -->
     </android.support.v4.widget.DrawerLayout>
    
    步骤4 初始化NavigationView并处理单击事件:

    public class MainActivity extends AppCompatActivity {
    
        NavigationView mNavigationView;
        DrawerLayout mDrawerLayout;
    
        // Other stuff
    
        private void init() {
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
            mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    mDrawerLayout.closeDrawers();
                    menuItem.setChecked(true);
                    switch (menuItem.getItemId()) {
                        case R.id.nav_home:
                            // TODO - Do something
                            break;
                        // TODO - Handle other items
                    }
                    return true;
                }
            });
        }
    }
    
    步骤5 确保在
    values-v21
    中设置
    android:windowdrawsystembarbackgrounds
    android:statusBarColor
    ,否则您的抽屉将不会显示在“状态栏”下

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Other attributes like colorPrimary, colorAccent etc. -->
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
    
    
    符合事实的
    @android:彩色/透明
    
    可选步骤 向NavigationView添加标题。为此,只需创建一个新布局并将
    app:headerLayout=“@layout/my\u header\u layout”
    添加到NavigationView

    后果

    笔记
    • 突出显示的颜色使用通过
      colorPrimary
      属性定义的颜色
    • 列表项使用通过
      textColorPrimary
      属性定义的颜色
    • 图标使用通过
      textColorSecondary
      属性定义的颜色
    您还可以查看Chris Banes的,其中突出显示了NavigationView以及设计支持库中的其他新视图(如FloatingActionButton、TextInputLayout、Snackbar、TableLayout等)。

    我正在使用设计支持库。只需使用自定义主题,我就可以在打开导航抽屉时获得透明的状态栏

    
    @颜色/原色
    @颜色/原色深色
    符合事实的
    @android:彩色/透明
    
    最后在清单文件中添加主题

    
    

    不要忘记使用“抽屉布局”中的属性,
    android:fitsystemwindows=“true”
    这里提到的所有答案都太旧太长了。使用最新Navigationview的最佳简短解决方案是

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        super.onDrawerSlide(drawerView, slideOffset);
    
        try {
            //int currentapiVersion = android.os.Build.VERSION.SDK_INT;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
                // Do something for lollipop and above versions
    
            Window window = getWindow();
    
            // clear FLAG_TRANSLUCENT_STATUS flag:
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    
            // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    
            // finally change the color to any color with transparency
    
             window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDarktrans));}
    
        } catch (Exception e) {
    
            Crashlytics.logException(e);
    
        }
    }
    
    这将在您打开抽屉时将状态栏颜色更改为透明

    现在,当你关闭抽屉时,你需要将状态栏的颜色再次更改为深色。这样你就可以用这种方法来完成

            public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            try {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
        // Do something for lollipop and above versions
    
        Window window = getWindow();
    
        // clear FLAG_TRANSLUCENT_STATUS flag:
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    
        // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    
        // finally change the color again to dark
        window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));}
        } catch (Exception e) {
        Crashlytics.logException(e);
                        }
                        }
    
    然后在主布局中添加一行,即

                android:fitsSystemWindows="true"
    
    您的抽屉布局将如下所示

                <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:fitsSystemWindows="true"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/navigation_header"
            app:menu="@menu/drawer"
            />
    
    
    
    您的导航视图如下所示

                <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:fitsSystemWindows="true"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/navigation_header"
            app:menu="@menu/drawer"
            />
    
    
    
    我已经对它进行了测试,它完全可以工作。希望它能帮助别人。这可能不是最好的方法,但它工作顺利,易于实现。
    如果有帮助,请标记它。快乐编码:)

    是的,
    DrawerLayout
    需要将其放置在系统栏后面。然后需要在抽屉视图s上设置它
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/navigation_header"
            app:menu="@menu/drawer"
            />