android获取屏幕大小,包括状态栏和软件导航栏的大小 [英] android get screen size including the size of status bar and software navigation bar

查看:128
本文介绍了android获取屏幕大小,包括状态栏和软件导航栏的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在包含导航栏和状态栏的情况下获取以像素为单位的屏幕尺寸?

How can I get the size of the screen in pixels with navigation bar and status bar being included?

我已经尝试使用<$ c $来获取尺寸c> DisplayMetrics ,但大小不包括软件导航栏。

I have already tried getting the size using DisplayMetrics but the size doesn't include the software navigation bar.

推荐答案

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

software navigation is added since API 17 (JELLY_BEAN_MR1) so we need the to include the size of navigation bar only in API 17 and above. and note that when you get the screen size it is based on the current orientation.

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;
}

链接到要点->此处

这篇关于android获取屏幕大小,包括状态栏和软件导航栏的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆