2016-08-23 63 views
0

我正在使用Xamarin在C#中开发应用程序。我用下面的代码来检测所述装置屏幕尺寸:使用软导航栏正确检测屏幕大小

var metrics = Resources.DisplayMetrics; 
int widthInDp = metrics.WidthPixels/Resources.DisplayMetrics.Density; 
int heightInDp = metrics.HeightPixels/Resources.DisplayMetrics.Density; 

在具有物理导航按钮的装置,并且没有软导航栏,(三星SM-E7000),此方法返回预期的结果:

360dp x 640dp in portrait mode 
640dp x 360dp in landscape mode 

然而,这并没有物理导航按钮,但软导航栏,(的Nexus 4)的设备上,该方法返回意外(我)结果:

384dp x 592dp in portrait mode 
598dp x 384dp in landscape mode 

它本身Nexus的ems结果允许48dp用于纵向模式下的软导航栏,42dp用于横向模式。

我想使用全屏 - 如果设备有一个软导航栏我想隐藏它并使用该空间。我可以隐藏软导航栏,但我需要能够检测实际的屏幕尺寸(例如Nexus 4的384dp x 640dp),或者设备是否有软导航栏(最好是其大小),所以我可以调整隐藏时可用的额外空间。

我见过几个计算器回答的问题对Java有类似代码:

public boolean hasNavBar (Resources resources) 
{ 
    int id = resources.getIdentifier("config_showNavigationBar", "bool", "android"); 
    return id > 0 && resources.getBoolean(id); 
} 

,但我无法找到C#/ Xamarin等同。

任何帮助表示赞赏。

回答

0

C#版本:

public bool hasNavBar(Resources resources) 
{ 
    int id = resources.GetIdentifier("config_showNavigationBar", "bool", "android"); 
    return id > 0 && resources.GetBoolean(id); 
} 
+0

谢谢你 - 感激。 – JeffR

相关问题