Xamarin.Android:在全屏模式下隐藏StatusBar和通知

时间:2018-08-27 06:19:56

标签: android xamarin xamarin.android

我正在尝试在全屏模式下隐藏状态栏和通知。用户应该不能在Xamarin android中拉/滑下状态/通知栏。

在本机android中,它可以使用以下属性(Android版本8.0)正常工作。

View decorView = getWindow().getDecorView(); 
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

getWindow().setFlags(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);

等效于xamarin中的 TYPE_APPLICATION_OVERLAY

是否有Xamarin.Android解决方案?

我尝试了以下xamarin的属性:

View decorView = Window.DecorView;
    var uiOptions = (int)decorView.SystemUiVisibility;
    var newUiOptions = (int)uiOptions;      
    newUiOptions |= (int)SystemUiFlags.HideNavigation;
    newUiOptions |= (int)SystemUiFlags.LayoutHideNavigation;
    newUiOptions |= (int)SystemUiFlags.LayoutFullscreen;
    newUiOptions |= (int)SystemUiFlags.Fullscreen;
    newUiOptions |= (int)SystemUiFlags.ImmersiveSticky; 
    decorView.SystemUiVisibility = (StatusBarVisibility)newUiOptions;

    Window.AddFlags(WindowManagerFlags.TranslucentNavigation);
    Window.AddFlags(WindowManagerFlags.TranslucentStatus);
    Window.ClearFlags(WindowManagerFlags.ForceNotFullscreen);

我们将不胜感激。谢谢

2 个答案:

答案 0 :(得分:0)

将OnCreate中的窗口标志更改为全屏应该可以解决问题

 this.Window.AddFlags(WindowManagerFlags.Fullscreen); //to show
 this.Window.ClearFlags(WindowManagerFlags.Fullscreen); //to hide

在不起作用的情况下还原。

更新

        if ((int)Android.OS.Build.VERSION.SdkInt >= 19)
        {
            var decorView = this.Window.DecorView;
            decorView.SystemUiVisibility = StatusBarVisibility.Hidden;// (StatusBarVisibility)newUiOptions;
        }
        else
        {
            var attrs = this.Window.Attributes;
            attrs.Flags |= WindowManagerFlags.Fullscreen;
            this.Window.Attributes = attrs;
        }

答案 1 :(得分:0)

此代码用于生成一个视图以覆盖statusBar区域,但不确定它是否适用于Oreo。

需要SYSTEM_ALERT_WINDOW权限。

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);

        WindowManagerLayoutParams localLayoutParams = new WindowManagerLayoutParams();

        localLayoutParams.Type = WindowManagerTypes.SystemError;
        localLayoutParams.Gravity = GravityFlags.Top;
        localLayoutParams.Flags = WindowManagerFlags.NotFocusable | 
            WindowManagerFlags.NotTouchModal | WindowManagerFlags.LayoutInScreen;
        localLayoutParams.Width = WindowManagerLayoutParams.MatchParent;
        localLayoutParams.Height = (int)(50 * Resources.DisplayMetrics.ScaledDensity);
        localLayoutParams.Format = Format.Transparent;

        IWindowManager manager = ApplicationContext.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
        customViewGroup view = new customViewGroup(this);

        manager.AddView(view, localLayoutParams);
    }
    public class customViewGroup : ViewGroup
    {
        public customViewGroup(Context context) : base(context)
        {
        }
        public override bool OnTouchEvent(MotionEvent ev)
        {
            return true;
        }
        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            // throw new NotImplementedException();
        }
    }

希望这会有所帮助。

原始Java代码源: https://stackoverflow.com/a/25397029/3814729

相关问题