Android:如何正确隐藏系统UI
作者:互联网
我正在开发一个视频播放器应用程序,我遇到了一个问题.我有一个自定义视频控制器,其中包含一个全屏按钮,我想在用户进入全屏模式时隐藏系统UI(导航和状态栏).我尝试过像this这样的东西,但它无法正常工作.我的应用程序如下面的截图:
当我单击全屏按钮时,我将方向更改为横向,我隐藏了系统UI,但我的播放器没有全屏显示.它看起来像下面的屏幕:
此外,当我点击屏幕时,我想显示我的视频控制器,但系统UI显示以下内容,而不是:
那么,我该如何实现这种行为呢?以下是我的全屏和隐藏/显示系统UI的方法:
@Override
public void toggleFullscreen() {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mVideoContainer.getLayoutParams();
if (!mFullscreen) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
hideSystemUI();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.setMargins(0, 0, 0, 0);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
showSystemUI();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
params.width = metrics.widthPixels;
params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, metrics);
params.setMargins(0, 0, 0, 0);
}
mVideoContainer.setLayoutParams(params);
mFullscreen = !mFullscreen;
}
private void hideSystemUI() {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
} else {
uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
}
decorView.setSystemUiVisibility(uiOptions);
}
private void showSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(0);
}
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/video_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/videoSurfaceContainer"
android:layout_width="match_parent"
android:layout_height="300dp" >
<SurfaceView
android:id="@+id/videoSurface"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
解决方法:
我将此包含在我想要隐藏导航栏和状态栏的每个活动中:
public void hideToolBr(){
// BEGIN_INCLUDE (get_current_ui_flags)
// The UI options currently enabled are represented by a bitfield.
// getSystemUiVisibility() gives us that bitfield.
int uiOptions = getWindow().getDecorView().getSystemUiVisibility();
int newUiOptions = uiOptions;
// END_INCLUDE (get_current_ui_flags)
// BEGIN_INCLUDE (toggle_ui_flags)
boolean isImmersiveModeEnabled =
((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
if (isImmersiveModeEnabled) {
Log.i(Constants.TAG_DEF, "Turning immersive mode mode off. ");
} else {
Log.i(Constants.TAG_DEF, "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;
}
// Immersive mode: Backward compatible to KitKat.
// Note that this flag doesn't do anything by itself, it only augments the behavior
// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample
// all three flags are being toggled together.
// Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
// Sticky immersive mode differs in that it makes the navigation and status bars
// semi-transparent, and the UI flag does not get cleared when the user interacts with
// the screen.
if (Build.VERSION.SDK_INT >= 18) {
newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
//END_INCLUDE (set_ui_flags)
}
然后在我的activity的onCreate方法中,我在setContentView()之后调用它
hideToolBr();
然后,用户所要做的就是从底部向上滑动或从顶部向下滑动以调出状态栏或导航栏.谷歌称之为“沉浸式模式”.它使开发人员充分利用设备的屏幕空间.
从Googles的沉浸式模式示例中,调用它显示系统UI:
// 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);
}
您可以在here找到更多信息.
编辑:
将它添加到styles.xml中
<style name="YourTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:colorForeground">@color/colorPrimaryDark</item>
</style>
标签:android,statusbar,fullscreen,navigationbar 来源: https://codeday.me/bug/20190608/1200263.html