隐藏Android状态栏

14
如何在Android 4.1.2上隐藏状态栏?我想要隐藏版本4.1.2上的状态栏,并在我的应用程序中实现隐藏状态栏。
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);    
layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

这段代码不能在4.1.2版本上运行。


你所说的状态栏是什么意思?是指标题栏吗? - Shakeeb Ayaz
看一下如何在Android中隐藏状态栏和如何隐藏状态栏而不隐藏标题栏的方法。 - Sunil Kumar Sahoo
谢谢,但是这不适用于4.2版本。 - Mohammad Alkhdour
8个回答

20

自从Jellybean(4.1)以来,有一种新的方法不依赖于WindowManager。而是使用窗口上的setSystemUiVisibility,这比使用WindowManager标志更具有细粒度控制系统栏的能力。以下是完整示例:

if (Build.VERSION.SDK_INT < 16) { //ye olde method
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else { // Jellybean and up, new hotness
    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();
    if(actionBar != null) {
         actionBar.hide();
    }
}

2
有用且幽默,点赞。 顺便提醒一下,记得检查actionBar是否为空,以防万一。 if (actionBar != null){ actionBar.hide(); } - Carlos Borau

12

将此添加到清单文件中的活动标记下,以隐藏状态栏

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

完成了 :)


我只想针对我的应用程序,而不是所有应用程序。 - Mohammad Alkhdour

8

在oncreate()方法中添加以下代码:

     requestWindowFeature(Window.FEATURE_NO_TITLE);// hide statusbar of Android
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

在 setContentView() 后调用它。 - M. Usman Khan
在调用 setContentView() 之前调用它。 - chakri Reddy

3

我认为这将有效

 public static void hideStatusBar(Activity activity) {
    WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
    attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
    activity.getWindow().setAttributes(attrs);
    activity.getWindow().clearFlags(
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
  }



public static void showStatusBar(Activity activity) {
    WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
    attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    activity.getWindow().setAttributes(attrs);
    activity.getWindow().addFlags(
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
  }

3
希望这是你要找的内容。在setContentView之前添加以下内容:
     requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

1
在您的onCreate方法中编写此代码。
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

1
他正在询问状态/通知栏而不是标题栏。 - Shakeeb Ayaz

0

在这个onWindowFocusChanged()方法中编写该行代码

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

0
这对我有效:
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_splash);
}

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