Xamarin.Forms android实现沉浸式
在android项目里,这样设置
using System; using Android.App; using Android.Content.PM; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; namespace Doubi.Droid { [Activity(Label = "Doubi", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; var uiOpts = SystemUiFlags.LayoutStable | SystemUiFlags.LayoutFullscreen; //LayoutStable表示布局稳定,不随其他变动而变动 //LayoutFullscreen表示把布局拓宽到全屏幕 Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOpts; //把标题栏设置为透明色 Window.SetStatusBarColor(Android.Graphics.Color.Argb(0,0,0,0)); base.OnCreate(bundle); Doubi.KLineView.Density = Resources.DisplayMetrics.Density; global::Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(new App()); } } }
更多UIFlag的说明:
Flag | 意义 |
---|---|
SYSTEM_UI_FLAG_LOW_PROFILE | 弱化状态栏和导航栏的图标 |
SYSTEM_UI_FLAG_HIDE_NAVIGATION | 隐藏导航栏,用户点击屏幕会显示导航栏 |
SYSTEM_UI_FLAG_FULLSCREEN | 隐藏状态栏 |
SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | 拓展布局到导航栏后面 |
SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | 拓展布局到状态栏后面 |
SYSTEM_UI_FLAG_LAYOUT_STABLE | 稳定的布局,不会随系统栏的隐藏、显示而变化 |
SYSTEM_UI_FLAG_IMMERSIVE | 沉浸模式,用户可以交互的界面 |
SYSTEM_UI_FLAG_IMMERSIVE_STICKY | 沉浸模式,用户可以交互的界面。同时,用户上下拉系统栏时,会自动隐藏系统栏 |