如何在Xamarin.Form应用程序中获取Android底部软件导航栏的高度?

sol*_*mer 2 android xamarin.android xamarin.forms

我很困惑的是,在不同的文档中,导航栏指的是不同的控件,但在这里,它是Android功能,除了硬件按钮之外还提供导航控件。我希望得到这个条宽码的高度。

在挖掘了有关永久隐藏栏的解决方案后,我意识到这是不可能的(这是解决问题的最理想的方法),如果栏可以永久隐藏,请纠正我!我在此处附加了我用来隐藏该栏的代码,但每当我点击屏幕时,该栏就会重新出现。

相反,我可以获取Android上底部软件导航栏的高度,以便我可以相应地调整页面元素的位置吗?(更令人烦恼的是,某些 Android 设备允许用户通过单击栏上的箭头按钮来手动显示和隐藏栏。)

//In OnCreate of MainActivity.cs,

int uiOptions = (int)Window.DecorView.SystemUiVisibility;
uiOptions |= (int)SystemUiFlags.HideNavigation;
Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;

//In some topics, people said to add some more SystemUiFlags, such as IMMERSIVE_STICKY, LAYOUT_FULLSCREEN, but with those, the bar cannot be hidden.
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述奇怪的是,DeviceDisplay.MainDisplayInfo.Height 包括导航栏的高度,否则我不需要执行上述操作。有没有一种方法可以在没有软件导航栏的情况下检测屏幕高度?

非常感谢您对此的任何帮助。

PS:我可以至少检测软件导航栏是否打开/显示,而不是获取高度吗?! 现在我知道我可以使用 ViewConfiguration.Get(Android.App.Application.Context).HasPermanentMenuKey 检测设备是否有硬导航键,这很有用,但我仍然无法检测软导航栏是否打开。

Che*_*SFT 5

关于在xamarin.form中获取导航栏高度,您可以尝试使用:

 TypedArray styledAttributes = this.Theme.ObtainStyledAttributes(new int[] {Android.Resource.Attribute.ActionBarSize });
        var actionbarHeight = (int)styledAttributes.GetDimension(0, 0);
        styledAttributes.Recycle();
        Console.WriteLine("the height is {0}",actionbarHeight);
Run Code Online (Sandbox Code Playgroud)

  • 最终,我走了这条路,在Android项目中将其放入依赖服务中:Resources res = Android.App.Application.Context.Resources; int resourceId = res.GetIdentifier("navigation_bar_height", "dimen", "android"); int height = res.GetDimensionPixelSize(resourceId); 返回高度; (2认同)