Android获取屏幕宽高,状态栏宽高,actionbar宽高,layout宽高,导航栏高度的方法汇总

看这个博客你可以知道

获取屏幕宽高,状态栏宽高,actionbar宽高,layout宽高,导航栏(虚拟按键栏)高度的方法

目录顺序为

代码测试的机型

状态栏高度

actionbar高度

屏幕高度

导航栏(虚拟按键栏)高度

layout宽高

总结

 

代码测试的机型:小米8青春版

这里我们用的是小米8青春版手机测试(刘海屏)

小米8青春版屏幕px为1080*2280,相当于360*760dp

//获取状态栏
private int getStatusBarHeight() {
    Class<?> c = null;
    Object obj = null;
    Field field = null;
    int x = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        return getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        Log.d(TAG, "get status bar height fail");
        e1.printStackTrace();
        return 75;
    }
}
Log.i(TAG, "onCreate: "+getStatusBarHeight());

上面的是获取状态栏方法,获得状态栏的高度(px)

因为这里用的小米手机是刘海屏,状态栏为20dp,通常手机屏幕的状态栏高度为25dp

dp=60/3=20dp

Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.i(TAG, "onPause: "+statusBarHeight);

上面的是第二种获取状态栏高度的方法(px)

P.s.该方法写在onCreate()中不可以,会获取到0

dp=60/3=20dp

//第一种获取方法
int actionBarHeight = getSupportActionBar().getHeight();


//第二种获取方法
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();

上面是获取Actionbar高度的方法(px)

dp=168/3=56dp 

P.s.该方法写在onCreate()中不可以,会获取到0

int screenWidth,screenHeight;
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
Log.i(TAG, "onCreate: "+screenWidth+","+screenHeight);

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth2 = dm.widthPixels;
int screenHeight2 = dm.heightPixels;
Log.i(TAG, "onCreate: "+screenWidth2+","+screenHeight2);

这是两种获取屏幕宽高的方法,单位为px

(获取到的屏幕为状态栏+actionbar+页面内容)

(不包含虚拟按键栏)

因为小米8青春版底部为虚拟按键栏,所以虚拟按键栏高度为2280-2150px=130px=43.3dp

//获取虚拟按键栏高度
    public static int getNavigationBarHeight(Context context, boolean b) {
        int result = 0;
        //是否纯在虚拟按键栏(导航栏)
        if (b) {
            Resources res = context.getResources();
            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = res.getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

上面是获取虚拟按键栏(导航栏)高度的方法(px) 

dp=130px/3=43.3dp

private ConstraintLayout constraintLayout;
constraintLayout=findViewById(R.id.screen_test);
constraintLayout.measure(0,0);
Log.i(TAG,"onCreate:"+constraintLayout.getMeasuredHeight()+","+constraintLayout.getMeasuredWidth());

上面是获取layout宽高的方法(px) ,这里用一个约束布局宽高都设置match_parent

P.s.该方法写在onCreate()中不可以,会获取到0

高dp=1922px/3=640.7dp

宽dp=1080px/3=360dp

注意点:尽量别在oncreate中获取各类高度,因为在Android界面创建过程中,容易读取不到宽高,会出现0的情况

总结:小米8青春版手机界面高度:

状态栏(StatusBar)        60px=20dp

ActionBar(标题栏)        168px=56dp

页面内容                       1922px=640.7dp

虚拟按钮栏(导航栏)       130px=43.3dp

共计2280px=760dp

___________________________________分割线______________________________________

再用我自己的华为P10 plus真机测试一下屏幕高度

自己平时用的华为P10 plus,  分辨率为1440*2560px,dp为360*640dp

状态栏(StatusBar)        24dp

ActionBar(标题栏)        56dp

页面内容                      560dp

高度共计640dp

华为P10 plus没有虚拟按键栏,也不是刘海屏,所以和上面的小米手机有点区别

猜你喜欢

转载自blog.csdn.net/yh18668197127/article/details/84970527