我正在尝试创建一个按钮,我可以隐藏或显示平板电脑上的状态栏。
我已经加入了onCreategetWindow().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,发现事情正在发生。
show: http : //pastebin.com/CidTRSTi隐藏: http : //pastebin.com/iPS6Kgbp
您是否在清单中设置了全屏主题?
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);
对于某些人,通过清除FLAG_FULLSCREEN显示状态栏可能不起作用,
这是适用于我的解决方案,( 文档 )( 标记参考 )
隐藏状态栏
// 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); }
KitKat中引入的function之一是“ 沉浸模式 ”。 沉浸式模式使用户能够通过滑动显示/隐藏状态栏和导航栏。 要尝试,请单击“切换沉浸式模式”按钮,然后尝试滑入和滑出该栏!
例:
public void toggleHideyBar() { int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility(); int newUiOptions = uiOptions; boolean isImmersiveModeEnabled = ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); if (isImmersiveModeEnabled) { Log.i(TAG, "Turning immersive mode mode off. "); } else { Log.i(TAG, "Turning immersive mode mode on."); } // Navigation bar hiding: Backwards compatible to ICS. if (Build.VERSION.SDK_INT >= 14) { newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; } // Status bar hiding: Backwards compatible to Jellybean if (Build.VERSION.SDK_INT >= 16) { newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN; } if (Build.VERSION.SDK_INT >= 18) { newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; } getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions); //END_INCLUDE (set_ui_flags) }
@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); }
// This snippet shows the system bars. It does this by removing all the flags // except for the ones that make the content appear under the system bars. private void showSystemUI() { mDecorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); }