【问题标题】:How to hide status bar?如何隐藏状态栏?
【发布时间】:2016-02-15 19:13:00
【问题描述】:

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

我发现了这个类似的问题,但没有一个答案对我有用。每次我尝试参加活动时,该应用程序都会崩溃: How to hide status bar in Android

谢谢。

【问题讨论】:

标签: android android-activity android-statusbar


【解决方案1】:

在设置内容之前在你的活动中尝试这个

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

【讨论】:

  • 这行得通,但它适用于所有版本的 Android 吗?
【解决方案2】:

在 Android 4.0 及更低版本上隐藏状态栏

  1. 通过在 manifest.xml 文件中设置应用程序的主题。

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

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

    @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();

【讨论】:

    【解决方案3】:
    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();
    }
    

    【讨论】:

    • 在 Android 9 上不适合我...该应用处于全屏模式,但显示状态栏。有什么想法吗?
    • 请在 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)); }
    【解决方案4】:

    唯一有效的答案(至少对我来说)

    在styles.xml中

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

    代码内解决方案在 4.4.2 Kitkat 中对我不起作用。

    【讨论】:

      【解决方案5】:

      打开 styles.xml 并更新您的活动使用的样式:

      <style name="ExampleTheme" parent="android:Theme.Light">
          <item name="android:windowNoTitle">true</item> <!-- add this line -->
      </style>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-17
        • 2013-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-29
        相关资源
        最近更新 更多