Window.DecorView.SystemUiVisibility
自 起已弃用Android R (11)
。在这个 API 版本中有一种新的方法可以做到这一点。
下面是我在我的一个应用程序中使用的代码。我不得不重写 MainActivity 类中的一堆方法。
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using Android.Views;
namespace SampleApp.Droid
{
[Activity(Label = "SampleApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
SetFullscreenFlags();
}
protected override void OnResume()
{
base.OnResume();
SetFullscreenFlags();
}
public override void OnWindowFocusChanged(bool hasFocus)
{
base.OnWindowFocusChanged(hasFocus);
SetFullscreenFlags();
}
protected void SetFullscreenFlags()
{
var attrs = Window.Attributes;
attrs.Flags |= WindowManagerFlags.Fullscreen;
Window.Attributes = attrs;
// BuildVersionCodes has a wrong value for Android R (1000 instead of 30)
// https://github.com/xamarin/xamarin-android/issues/5723
// if (Build.VERSION.SdkInt >= BuildVersionCodes.R)
if ((int)Build.VERSION.SdkInt >= 30)
{
Window.SetDecorFitsSystemWindows(false);
if (Window.InsetsController != null)
{
Window.InsetsController.Hide(WindowInsets.Type.NavigationBars());
Window.InsetsController.Hide(WindowInsets.Type.StatusBars());
Window.InsetsController.Hide(WindowInsets.Type.SystemBars());
Window.InsetsController.Show(WindowInsets.Type.Ime());
}
}
else
{
var uiOptions = (int)Window.DecorView.SystemUiVisibility;
var newUiOptions = (int)uiOptions;
newUiOptions |=
(int)SystemUiFlags.LayoutStable |
(int)SystemUiFlags.LayoutHideNavigation |
(int)SystemUiFlags.LayoutFullscreen |
(int)SystemUiFlags.HideNavigation |
(int)SystemUiFlags.Fullscreen |
(int)SystemUiFlags.ImmersiveSticky;
Window.DecorView.SystemUiVisibility = (StatusBarVisibility)newUiOptions;
}
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
之前,我也实现了View.IOnSystemUiVisibilityChangeListener
接口。但它也已被弃用,并将在未来的版本中删除:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, View.IOnSystemUiVisibilityChangeListener
{
protected override void OnCreate(Bundle savedInstanceState)
{
// omitted code
Window.DecorView.SetOnSystemUiVisibilityChangeListener(this);
}
public void OnSystemUiVisibilityChange([GeneratedEnum] StatusBarVisibility visibility)
{
SetFullscreenFlags();
}
}
编辑:
如果您想在用户将某些输入控件聚焦在您的屏幕上后保持沉浸式模式,您可以实现ViewTreeObserver.IOnGlobalFocusChangeListener
界面。这是我的做法:
public class MainActivity :
global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity,
ViewTreeObserver.IOnGlobalFocusChangeListener
{
protected override void OnCreate(Bundle savedInstanceState)
{
// ommited code
}
public override void SetContentView(View view)
{
base.SetContentView(view);
if (view?.ViewTreeObserver != null)
view.ViewTreeObserver.AddOnGlobalFocusChangeListener(this);
}
public override void SetContentView(View view, ViewGroup.LayoutParams @params)
{
base.SetContentView(view, @params);
if (view?.ViewTreeObserver != null)
view.ViewTreeObserver.AddOnGlobalFocusChangeListener(this);
}
public void OnGlobalFocusChanged(View oldFocus, View newFocus)
{
SetFullscreenFlags();
}
}