我正在开发一个视频播放器应用程序,我遇到了一个问题。我有一个自定义视频控制器,其中包含一个全屏按钮,我想在用户进入全屏模式时隐藏系统UI(导航和状态栏)。我试图做this之类的事情,但它无法正常工作。我的应用程序如下面的截图:
当我点击全屏按钮时,我将方向改为横向,我隐藏了系统用户界面,但我的播放器没有全屏显示。它看起来像下面的屏幕:
此外,当我点击屏幕时,我想显示我的视频控制器,但系统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>
答案 0 :(得分:5)
我将此包含在我想要隐藏导航栏和状态栏的每个活动中:
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)
}
然后在我的活动onCreate方法中,我在setContentView()
hideToolBr();
然后,用户只需从底部向上滑动或从顶部向下滑动即可调出状态栏或导航栏。谷歌称之为“沉浸式模式”#34;它使开发人员可以充分利用这些设备。屏幕房地产。
从谷歌沉浸式模式的例子中,调用它显示系统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>
答案 1 :(得分:3)
要隐藏/显示系统UI时只需调用这些方法。
private void hideSystemUI() {
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = decorView.getSystemUiVisibility();
int newUiOptions = uiOptions;
newUiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
newUiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
newUiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
newUiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE;
newUiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(newUiOptions);
}
private void showSystemUI() {
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = decorView.getSystemUiVisibility();
int newUiOptions = uiOptions;
newUiOptions &= ~View.SYSTEM_UI_FLAG_LOW_PROFILE;
newUiOptions &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
newUiOptions &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
newUiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE;
newUiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(newUiOptions);
}
答案 2 :(得分:0)
转到 https://developer.android.com/training/system-ui/immersive
他们已经非常正确地解释了该系统。 并在您的活动主题中添加以下样式
<style name="BlackActionTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/black</item>
<item name="colorPrimaryDark">@color/black</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
</style>
答案 3 :(得分:0)
对于Kotlin:
private fun hideSystemUI() {
val decorView: View = this.window.decorView
val uiOptions = decorView.systemUiVisibility
var newUiOptions = uiOptions
newUiOptions = newUiOptions or View.SYSTEM_UI_FLAG_LOW_PROFILE
newUiOptions = newUiOptions or View.SYSTEM_UI_FLAG_FULLSCREEN
newUiOptions = newUiOptions or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
newUiOptions = newUiOptions or View.SYSTEM_UI_FLAG_IMMERSIVE
newUiOptions = newUiOptions or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
decorView.systemUiVisibility = newUiOptions
}
private fun showSystemUI() {
val decorView: View = this.window.decorView
val uiOptions = decorView.systemUiVisibility
var newUiOptions = uiOptions
newUiOptions = newUiOptions and View.SYSTEM_UI_FLAG_LOW_PROFILE.inv()
newUiOptions = newUiOptions and View.SYSTEM_UI_FLAG_FULLSCREEN.inv()
newUiOptions = newUiOptions and View.SYSTEM_UI_FLAG_HIDE_NAVIGATION.inv()
newUiOptions = newUiOptions and View.SYSTEM_UI_FLAG_IMMERSIVE.inv()
newUiOptions = newUiOptions and View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY.inv()
decorView.systemUiVisibility = newUiOptions
}
答案 4 :(得分:0)
在 API 版本 30 (R) 中,设置 window.decorView.systemUiVisibility
已弃用。改用 window.insetsController
:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.let {
it.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
window.navigationBarColor = getColor(R.color.semi_transparent)
it.hide(WindowInsets.Type.systemBars())
}
} else {
// see other answers
}
详细解释见https://medium.com/swlh/modifying-system-ui-visibility-in-android-11-e66a4128898b