在 android 11 上,状态栏不会隐藏 [英] On android 11 status bar doesn't get hide

查看:97
本文介绍了在 android 11 上,状态栏不会隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码使状态栏在 Android 10 及更早版本上透明

This code makes status bar transparent on Android 10 and before

//使actionbar透明activity.window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

但是在 android 11 中它不会消失

However in android 11 it doesn't disappear

安卓 11

安卓 8

我的styles.xml 文件是这样的

My styles.xml file look like this

<resources xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<style name="DropitTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>


    <style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
        <!-- color used by navigation icon and overflow icon -->
        <item name="colorOnPrimary">@color/colorPrimary</item>
    </style>

    <style name="Toolbar_text_style" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
        <!-- color used by navigation icon and overflow icon -->
        <item name="android:textSize">@dimen/textSizeHuge</item>
    </style>
</resources>

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/DropitTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

</manifest>

最后是 themes.xml

and last the themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <!-- Base application theme. -->
    <style name="Theme.Dropit" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryVariant">@color/colorSecondary</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/colorSecondary</item>
        <item name="colorSecondaryVariant">@color/colorSecondaryLight</item>
        <item name="colorOnSecondary">@color/white</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

正如您在清单中看到的,我使用的是样式主题而不是主题主题.那么我该如何解决这个问题?

As you can see in the Manifest I'm using the styles theme not the themes theme. So how can i fix this problem?

推荐答案

我遇到了同样的问题并找到了这个解决方案.

I had the same problem and find out this solution.

val controller = requireActivity().window.insetsController
        controller?.systemBarsBehavior = BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        controller?.hide(WindowInsets.Type.statusBars())

如果从 Activity 调用,则不需要 requireActivity().我遇到了一些问题,在某些片段上它开始重置为标准状态栏,所以我在每个 onCreate() 上调用它并修复它.

If you call it from Activity requireActivity() is not needed. I had some problems where on some fragments it started to reset to the standard status bar, so I called it on every onCreate() and it fixed it.

这篇关于在 android 11 上,状态栏不会隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆