【问题标题】:android get screen size including the size of status bar and software navigation barandroid获取屏幕大小包括状态栏和软件导航栏的大小
【发布时间】:2014-10-31 12:08:44
【问题描述】:

如何获取包含导航栏和状态栏的屏幕大小(以像素为单位)?

我已经尝试使用DisplayMetrics 获取大小,但大小不包括软件导航栏。

【问题讨论】:

    标签: android android-screen


    【解决方案1】:

    从 API 17 (JELLY_BEAN_MR1) 开始添加了软件导航,因此我们只需要在 API 17 及更高版本中包含导航栏的大小。 请注意,当您获得屏幕尺寸时,它基于当前方向。

    public void setScreenSize(Context context) {
        int x, y, orientation = context.getResources().getConfiguration().orientation;
        WindowManager wm = ((WindowManager) 
            context.getSystemService(Context.WINDOW_SERVICE));
        Display display = wm.getDefaultDisplay();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            Point screenSize = new Point();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                display.getRealSize(screenSize);
                x = screenSize.x;
                y = screenSize.y;
            } else {
                display.getSize(screenSize);
                x = screenSize.x;
                y = screenSize.y;
            }
        } else {
            x = display.getWidth();
            y = display.getHeight();
        }
    
        int width = getWidth(x, y, orientation);
        int height = getHeight(x, y, orientation);
    }
    
    private int getWidth(int x, int y, int orientation) {
        return orientation == Configuration.ORIENTATION_PORTRAIT ? x : y;
    }
    
    private int getHeight(int x, int y, int orientation) {
        return orientation == Configuration.ORIENTATION_PORTRAIT ? y : x;
    }
    

    link to gist -> here

    【讨论】:

      猜你喜欢
      • 2013-06-21
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多