【问题标题】:Detecting screensize correctly with soft navigation bar使用软导航栏正确检测屏幕尺寸
【发布时间】:2016-12-29 23:43:07
【问题描述】:

我正在使用 Xamarin 在 C# 中开发一个应用程序。我使用以下代码来检测设备屏幕尺寸:

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

在具有物理导航按钮且没有软导航栏的设备上(Samsung 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,结果似乎允许纵向模式下的软导航栏为 48dp,横向模式下为 42dp。

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

我已经看到一些针对 Java 的 stackoverflow 问题的回答类似于:

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

但我找不到 C#/Xamarin 等效项。

任何帮助表示赞赏。

【问题讨论】:

    标签: c# android xamarin


    【解决方案1】:

    C#版本:

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

    【讨论】:

    • 谢谢 - 感激不尽。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2016-05-12
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多