Android:显示/隐藏状态栏/电源栏

时间:2011-11-25 18:51:19

标签: android show-hide statusbar

我正在尝试创建一个按钮,我可以在其中隐藏或显示平板电脑上的状态栏。

我已加入onCreate

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

并在按钮中 显示:

WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);

隐藏:

WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);

任何提示/ tipps?

//修改

我在这里查看了这个提示:http://android.serverbox.ch/?p=306 并改变了我的代码:

private void hideStatusBar() throws IOException, InterruptedException {
    Process proc = Runtime.getRuntime().exec(new String[]{"su","-c","service call activity 79 s16 com.android.systemui"});
    proc.waitFor();
}

private void showStatusBar() throws IOException, InterruptedException {
    Process proc = Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"});
    proc.waitFor();
}

因此,如果我点击我的按钮,方法被调用我可以看到发生了什么,因为应用程序正在等待几秒钟。 我也调查了LockCat,发现事情正在发生。

显示:http://pastebin.com/CidTRSTi 隐藏:http://pastebin.com/iPS6Kgbp

10 个答案:

答案 0 :(得分:112)

您是否在清单中设置了全屏主题?

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

如果没有这个,我认为你不能全屏。

我会使用以下内容添加和删除全屏标志:

// Hide status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Show status bar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

答案 1 :(得分:32)

对于某些人,通过清除FLAG_FULLSCREEN显示状态栏可能不起作用,

以下是适合我的解决方案,(Documentation)(Flag Reference

隐藏状态栏

// Hide Status Bar
if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
   View decorView = getWindow().getDecorView();
  // Hide Status Bar.
   int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
   decorView.setSystemUiVisibility(uiOptions);
}

显示状态栏

   if (Build.VERSION.SDK_INT < 16) {
              getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    else {
       View decorView = getWindow().getDecorView();
      // Show Status Bar.
       int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
       decorView.setSystemUiVisibility(uiOptions);
    }

答案 2 :(得分:2)

对于Kotlin用户

要显示

Class MyClass { var currentid: Int32? } NotificationCenter.default.addObserver(forName: ID_TRANSFER, object: nil, queue: nil){ notification in if let object = notification.object as? [String: Any] { if let currentidVal = object["UserID"] as? Int { currentid = currentidVal } } }

隐藏

activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

答案 3 :(得分:2)

我知道这是一个非常古老的问题,但以防万一有人在这里寻找最新的支持方式以编程方式隐藏状态栏,您可以按如下方式进行:

window.insetsController?.hide(WindowInsets.Type.statusBars())

并再次显示:

window.insetsController?.show(WindowInsets.Type.statusBars())

答案 4 :(得分:1)

用于Android中的Kolin 用于kolin中的隐藏状态栏,无需在行尾使用

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

在android中使用Java语言隐藏状态栏

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

答案 5 :(得分:1)

我尝试了很多事情。

最后,它是最适合隐藏和显示全屏模式的代码。

private fun hideSystemUi() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window.setDecorFitsSystemWindows(true)
    } else {
        // hide status bar
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_IMMERSIVE or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    }

}

private fun showSystemUi() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window.setDecorFitsSystemWindows(false)
    } else {
        // Show status bar
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.decorView.systemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_STABLE
    }

}

它是在此应用中实现的: Android Breakdown

转到视频(底部栏)>播放任何视频>切换全屏

答案 6 :(得分:0)

答案 7 :(得分:0)

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //hide status bar
    requestWindowFeature( Window.FEATURE_NO_TITLE );
    getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN );


    setContentView(R.layout.activity_main);
}

答案 8 :(得分:0)

fun Activity.setStatusBarVisibility(isVisible: Boolean) {
    //see details https://developer.android.com/training/system-ui/immersive
    if (isVisible) {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    } else {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }
}

答案 9 :(得分:0)

使用此方法,使用SYSTEM_UI_FLAG_IMMERSIVE_STICKY,只需轻按一下即可返回全屏,而无需执行任何操作。只需复制下面的此方法,然后在活动中的任意位置调用它即可。 更多详细信息here

private void hideSystemUI() {
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_IMMERSIVE
                    // Set the content to appear under the system bars so that the
                    // content doesn't resize when the system bars hide and show.
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    // Hide the nav bar and status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN);
}