如何隐藏状态栏?

12

我该如何隐藏特定活动的状态栏?

我找到了一个类似的问题,但没有一个答案适用于我的情况。每次尝试访问该活动时,应用程序都会崩溃: 如何在Android中隐藏状态栏

谢谢。


http://stackoverflow.com/questions/33692388/android-remove-actionbar-title-keeping-toolbar-menu/33692402#33692402 - Zubair Akber
状态栏是我想要移除的,而不是操作栏。 - user5495265
5个回答

23

在设置内容之前,尝试在您的活动中使用此代码

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

那个可以用,但是它适用于所有版本的Android吗? - user5495265

4

隐藏Android 4.0及以下版本的状态栏

  1. By setting the theme of application in manifest.xml file.

    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"
    

    OR

  2. By writing JAVA code in activity's onCreate() method.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // If the Android version is lower than Jellybean, use this call to hide
        // the status bar.
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(R.layout.activity_main);
    }
    

隐藏Android 4.1及更高版本的状态栏

通过在Activity的onCreate()方法中编写JAVA代码。

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();

1
if (Build.VERSION.SDK_INT < 16)//before Jelly Bean Versions
{ 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else // Jelly Bean and up
{ 
    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int ui = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(ui);

    //Hide actionbar
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}

在安卓9上对我不起作用... 应用程序是全屏的,但状态栏仍然显示出来。有任何想法吗? - Maxouille
1
请在OnCreate中的setContentView之前添加以下内容:if (Build.VERSION.SDK_INT >= 21) { Window window = this.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.setStatusBarColor(ContextCompat.getColor(this, R.color.toolbar_color)); } - Habibur Rahman Ovie

0

只有这个方法对我有效

在styles.xml文件中

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
    </style>
</resources>

在4.4.2 Kitkat中,代码解决方案对我无效。


-1
打开styles.xml文件并更新你的活动所使用的样式:
<style name="ExampleTheme" parent="android:Theme.Light">
    <item name="android:windowNoTitle">true</item> <!-- add this line -->
</style>

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