如何在安卓系统中隐藏状态栏

137
我参考了这个链接 link。如果用户点击EditText(例如“到:”),键盘将弹出,同时用户可以滚动查看屏幕上的所有其他视图(例如撰写、主题、发送按钮)。同样,在我的应用程序中,我有一个活动,在其中我拥有一些小部件或视图。 假设用户点击我的Activity中的EditText,则会弹出键盘,并且我可以滚动以查看其余视图。但是,如果我在清单中添加此属性android:theme="@android:style/Theme.NoTitleBar.Fullscreen",我无法滚动来查看其余视图。但是,如果我像这样在清单中添加属性android:theme="@android:style/Theme.NoTitleBar",那么我就可以滚动以查看其余视图,但该屏幕上会出现状态栏。我希望能够全屏并且即使弹出键盘也可以滚动查看其他视图...?我需要做哪些更改..?

3
请注意,在Android 3.0中,您无法隐藏状态栏,并且有很大可能您将来也无法隐藏状态栏。在Android 3.0中,状态栏包含后退和主页按钮,因此状态栏必须始终可用。 - CommonsWare
我刚刚成功地在3.0版本,API级别11的模拟器上隐藏了标题栏和状态栏。 - erdomester
全屏应该仍然允许,用户通常不想在玩全屏游戏时看到状态栏。 - Sam YC
@user448250 你最终找到答案了吗? - Bitcoin Cash - ADA enthusiast
活动是什么?它是您网站文件夹中的某个XML文件吗? - Nebular Dust
37个回答

6

manifest.xml文件中更改应用程序的主题。

android:theme="@android:style/Theme.Translucent.NoTitleBar"

奇怪的是,这在屏幕顶部显示了一个橙色的条形图。但通知栏却被隐藏了。 - Danish Ashfaq

5
在 AndroidManifest.xml 中,您需要在要使用的活动中添加以下内容:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
//this is for hiding action bar

在MainActivity.java中 -> 在onCreate()方法内,添加以下内容:

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//this is for hiding status bar

5
请使用以下代码:
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.youractivityxmlname);

5

对我来说,这是最好的解决方案,只需在您的theme.xml中编写此行。

<style name="MyApp" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
...
</style>

5
您可以使用 styles.xml 进行隐藏。
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- 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="HiddenTitleTheme" parent="AppTheme">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

只需在您的清单中像这样调用:android:theme="@style/HiddenTitleTheme"

5

这段代码可以隐藏状态栏。

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

要隐藏操作栏,请写入以下行:-
requestWindowFeature(Window.FEATURE_NO_TITLE);

这两行代码可以一起写以隐藏操作栏和状态栏。在onCreate方法中调用setContentView方法之前,必须写好这些代码。


运行得非常好。谢谢! - Joshua Pinter

4

您可以通过使用xml将状态栏的颜色设置为透明来隐藏它。在您的活动主题中添加statusBarColor项:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

请增加一些解释。仅有代码的答案可能会令其他人感到困惑。 - Ram Ghadiyaram

4

由于FLAG_FULLSCREEN在Android R中已被弃用,因此您可以使用以下代码隐藏状态栏。

 @Suppress("DEPRECATION")
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

           window.insetsController?.hide(WindowInsets.Type.statusBars())
    } else {

           window.setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
            )
        }

1
这是不正确的,它们的行为不同,插入只是像getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);这样。 - Bitwise DEVS

3

隐藏状态栏

private void hideSystemBars() {
    WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
    WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
    if (windowInsetsController == null) {
        return;
    }
    // Configure the behavior of the hidden system bars
    windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    // Hide both the status bar and the navigation bar
    windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
}

显示状态栏

private void showSystemBars() {
    WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
    WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
    if (windowInsetsController == null) {
        return;
    }
    // Configure the behavior of the hidden system bars
    windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    // Hide both the status bar and the navigation bar
    windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
}

为了顶部相机与屏幕的剪裁
<item name="android:windowLayoutInDisplayCutoutMode" tools:ignore="NewApi">shortEdges</item>

2

我坚持采用干净且可扩展的方法来显示和隐藏系统UI,这适用于不同的Android API级别:

object SystemBarsCompat {
    private val api: Api =
        when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> Api31()
            Build.VERSION.SDK_INT == Build.VERSION_CODES.R -> Api30()
            else -> Api()
        }

    fun hideSystemBars(window: Window, view: View, isImmersiveStickyMode: Boolean = false) =
        api.hideSystemBars(window, view, isImmersiveStickyMode)

    fun showSystemBars(window: Window, view: View) = api.showSystemBars(window, view)

    fun areSystemBarsHidden(view: View): Boolean = api.areSystemBarsHidden(view)

    @Suppress("DEPRECATION")
    private open class Api {
        open fun hideSystemBars(window: Window, view: View, isImmersiveStickyMode: Boolean = false) {
            val flags = View.SYSTEM_UI_FLAG_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

            view.systemUiVisibility = if (isImmersiveStickyMode) {
                flags or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            } else {
                flags or
                    View.SYSTEM_UI_FLAG_IMMERSIVE or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            }
        }

        open fun showSystemBars(window: Window, view: View) {
            view.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        }

        open fun areSystemBarsHidden(view: View) = view.systemUiVisibility and View.SYSTEM_UI_FLAG_HIDE_NAVIGATION != 0
    }

    @Suppress("DEPRECATION")
    @RequiresApi(Build.VERSION_CODES.R)
    private open class Api30 : Api() {

        open val defaultSystemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_SWIPE

        override fun hideSystemBars(window: Window, view: View, isImmersiveStickyMode: Boolean) {
            window.setDecorFitsSystemWindows(false)
            view.windowInsetsController?.let {
                it.systemBarsBehavior =
                    if (isImmersiveStickyMode) WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                    else defaultSystemBarsBehavior
                it.hide(WindowInsets.Type.systemBars())
            }
        }

        override fun showSystemBars(window: Window, view: View) {
            window.setDecorFitsSystemWindows(false)
            view.windowInsetsController?.show(WindowInsets.Type.systemBars())
        }

        override fun areSystemBarsHidden(view: View) = !view.rootWindowInsets.isVisible(WindowInsets.Type.navigationBars())
    }

    @RequiresApi(Build.VERSION_CODES.S)
    private class Api31 : Api30() {
        override val defaultSystemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT
    }
}

比如要隐藏系统栏,可以从Fragment中调用:

SystemBarsCompat.hideSystemBars(requireActivity().window, view)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接