【问题标题】:Android DrawerLayout (with NavigationView) behind status bar状态栏后面的Android DrawerLayout(带NavigationView)
【发布时间】:2017-01-06 08:37:40
【问题描述】:

我有一个带有DrawerLayout 的应用程序,其中包含NavigationView

activity_layout.xml:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/activity_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.exmaple.appname.activityname">

        <android.support.v7.widget.Toolbar
            android:id="@+id/actionbar_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/Theme.AppCompat"/>

        […]

    </android.support.constraint.ConstraintLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_drawer"
        android:fitsSystemWindows="true"/>

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

我还添加了以下样式:

styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    […]

    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style>

values-21/styles.xml:

<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

所有这些代码都基于Google I/O Schedule App,但是DrawerLayout 仍然不会在状态栏后面呈现。关于为什么这种解决方案组合不起作用以及哪些解决方案可能起作用的任何想法?我发现在状态栏后面绘图从来都不是一刀切的解决方案,但是在尝试了 Stack Overflow 上我能想到的所有解决方案组合之后,我仍然无法得到它。

当前抽屉图片:

【问题讨论】:

    标签: android drawerlayout navigationview android-statusbar


    【解决方案1】:

    只需在values-21/styles.xml 中添加&lt;item name="android:windowTranslucentStatus"&gt;true&lt;/item&gt; 并删除此&lt;item name="android:statusBarColor"&gt;@android:color/transparent&lt;/item&gt;

    在 xml 中将 android:fitsSystemWindows="true" 添加到您的 NavigationView

    工作示例!

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
    

    结果

    已编辑(主要布局)

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        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" <--This is mandatory 
        tools:context=".ui.activities.MainActivity">
    
        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white" />
    
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/colorPrimary"
            android:fitsSystemWindows="true" /><--This is mandatory 
    
    </android.support.v4.widget.DrawerLayout>
    

    当前结果

    更新:我查找您的 style-v21 并在下面找到

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="AppTheme.NoActionBar" parent="AppTheme">
            <item name="android:windowDrawsSystemBarBackgrounds">true</item>
            <item name="android:windowTranslucentStatus">true</item>
        </style>
    </resources>
    

    请换成这个

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
    

    【讨论】:

    • 不。我将statusBarColor 切换为windowTranslucentStatus(在v21 中)没有运气。然后我将fistsSystemWindows 添加到NavigationView(尽管我没有使用标题,也不希望它位于状态栏后面),但仍然没有。 NavigationView 的背景颜色仍然只绘制到状态栏。我将尽快提供完整的代码示例。
    • (以上更正:我确实希望NavigationViewfitsSystemWindow,因为这是我想在状态栏后面扩展的背景)
    • 更新了原始问题中的图像。我想要您展示的内容,但没有标题图片。抽屉的背景颜色应该延伸到状态栏的后面。
    • 是的,我之前在NavView 上尝试过fitsSystemWindows,但没有解决任何问题。有机会我会发布该代码示例。
    • 添加了演示仓库的链接。演示应用在我的测试设备上显示完全相同的问题,运行 7.1.1
    猜你喜欢
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多