Android NavigationDrawer按状态栏重叠

时间:2016-05-27 19:39:19

标签: android navigation-drawer statusbar

我已经看到很多关于在导航栏上方设置半透明状态栏的问题,如下所示:

enter image description here

但我想要实现的是拥有一个非半透明的状态栏,并使导航抽屉标题视图与操作栏的高度相同。这适用于前棒棒糖设备(API< 21)。但是对于API 21+,整个导航抽屉刚刚向上移动并与状态栏重叠,而不是位于状态栏下方。我能想到的唯一解决方法是在API 21+之后的设备的标题视图上手动设置layout_marginTop。有没有更好的解决方案来解决所有设备?

API< 21:

enter image description here

API> 21:

enter image description here

styles.xml(V21):

<resources>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

DrawerLayout:

    

    <include
        layout="@layout/app_bar_main"
        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="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

我已尝试将fitsSystemWindows设置为truefalse。没有变化。

初始化导航抽屉时,这是我的黑客解决方案(这也是@AkashBhave回答):

    LinearLayout navigationHeader = (LinearLayout) navigationView.getHeaderView(0);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        // This is make sure navigation header is below status bar
        // This only required for devices API >= 21
        LinearLayout.LayoutParams layoutParams =
                (LinearLayout.LayoutParams) navigationHeader.getLayoutParams();
        layoutParams.setMargins(0, getStatusBarHeight(), 0, 0);
        navigationHeader.setLayoutParams(layoutParams);
    }

5 个答案:

答案 0 :(得分:5)

确保fitsSystemWindows不仅在false中,而且在您的根NavigationView元素以及任何内容布局中设置为DrawerLayout

我在这个问题上挣扎了一段时间,直到我注意到android:fitsSystemWindows="true"已经在旧的根布局元素中设置了DrawerLayout,然后我将其包裹在android:fitsSystemWindows内。

我删除了<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.design.widget.CoordinatorLayout 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" tools:context=".ui.ImprintActivity"> <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" app:theme="@style/AppTheme.Toolbar" app:titleTextAppearance="@style/Toolbar.TitleText" app:contentInsetStartWithNavigation="0dp" app:contentInsetStart="0dp" > <include layout="@layout/toolbar_logo_small"/> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_imprint"/> </android.support.design.widget.CoordinatorLayout> <android.support.design.widget.NavigationView xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/navigation_view" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_gravity="start" app:headerLayout="@layout/navigation_view_header" app:menu="@menu/navigation_view_menu" android:background="@color/primary_light" /> </android.support.v4.widget.DrawerLayout> 的所有出现,状态栏也得到了API 21+的尊重。

{{1}}

答案 1 :(得分:2)

我认为您应该将margin_top属性设置为高于API 21 +的设备。您可以使用以下命令检索系统API级别:

Build.VERSION_CODES.LOLLIPOP

您可以检查并设置它:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    params.setMargins(left, top, right, bottom);
    yourbutton.setLayoutParams(params);
}

希望它有所帮助!

答案 2 :(得分:0)

您可以尝试在打开抽屉时将状态栏颜色设置为透明 -

getWindow().setStatusBarColor(Color.TRANSPARENT);

答案 3 :(得分:0)

尝试此 android:fitsSystemWindows =“ false”

<androidx.drawerlayout.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="false"
tools:openDrawer="start">

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

<com.google.android.material.navigation.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" />

答案 4 :(得分:0)

添加->在DrawerLayout中:

android:fitsSystemWindows="true"

NavigationView中的

android:fitsSystemWindows="false"