android - android 中 getRealMetrics 获取屏幕原始尺寸的替代方法是什么

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

在我的 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的设备有软导航栏。

最佳答案

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;

关于android - android 中 getRealMetrics 获取屏幕原始尺寸的替代方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31427897/

相关文章:

java - 设备上的 Lollipop 更新后 Android GCM 无法工作

android - 在没有硬件 key 的设备上打开选项菜单

android-architecture-components - 在导航组件的起始目标位置时如何处理后退按钮

Android:Fragment 的 onOptionsItemSelected 没有被调用

android - 抽屉导航更改突出显示项目的图标和文本颜色

java - 当我通过 NavController.navigateUp() 返回时,为什么我的 fragment *有时* 是空白的?

android - ListView中图片的延迟加载

Android在Andengine中为不同类型的实体实现对象池

android - fragment 对话框中的 fragment 膨胀引发错误 "Fragment did not create a view"

android - 单击按钮后如何创建菜单组?