【问题标题】:What is the alternative for getRealMetrics in android to get screen original sizeandroid中getRealMetrics获取屏幕原始尺寸的替代方法是什么
【发布时间】:2015-10-04 08:18:03
【问题描述】:

在我的 android 应用程序中,我需要确定设备是否有导航栏。为此,我正在获取设备原始屏幕大小和应用程序窗口大小。基于此,我正在计算差异,因此我可以确定设备是否具有导航栏。 这是我使用的代码:

public static boolean hasSoftKeys(WindowManager windowManager){
    Display d = windowManager.getDefaultDisplay();

    DisplayMetrics realDisplayMetrics = new DisplayMetrics();
    d.getRealMetrics(realDisplayMetrics);

    int realHeight = realDisplayMetrics.heightPixels;
    int realWidth = realDisplayMetrics.widthPixels;

    DisplayMetrics displayMetrics = new DisplayMetrics();
    d.getMetrics(displayMetrics);

    int displayHeight = displayMetrics.heightPixels;
    int displayWidth = displayMetrics.widthPixels;

    return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}

问题是:调用“getRealMetrics”方法需要 api 级别 17。这里我需要一个针对低版本设备的解决方案,它会给出与getRealMetrics 相同的结果来获得原始屏幕尺寸。我没有找到任何解决方案。

任何人都可以向我推荐任何适用于较低版本设备的getRealMetrics 替代方案吗?

这是我对导航栏可用性的调查。这并不是在所有设备上的可靠结果。

代码1:

boolean hasNavBar(Context context) {

        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        return !hasMenuKey && !hasBackKey;
    }

代码2

boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();

通过使用此代码,我们可以检查设备是否具有 PermanentMenuKey。但是没有PermanentMenuKey 的设备有软导航栏并不代表这个意思。

【问题讨论】:

标签: android android-menu android-navigation android-windowmanager


【解决方案1】:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;

【讨论】:

  • 这将给出应用程序活动占用的窗口大小。但不是窗口的原始大小。我有相同的代码。请验证问题中添加的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多