android获取手机状态栏高度,Android获取手机屏幕、状态栏、Activity显示区域的宽高的获取...

该博客介绍了如何在Android中获取手机屏幕的宽度和高度,包括状态栏的高度。通过DisplayMetrics类和getWindowVisibleDisplayFrame()方法,可以精确地得到屏幕和状态栏的尺寸信息。此外,还探讨了在不同设备上,如带有SmartBar的设备,如何获取应用区域的尺寸。
摘要由CSDN通过智能技术生成

1、手机屏幕宽高的获取

DisplayMetrics metric = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(metric);

int width = metric.widthPixels; // 屏幕宽度(像素)

int height = metric.heightPixels; // 屏幕高度(像素)

2、状态栏宽高的获取

Rect outRect = new Rect();

activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);

outRect.top即是状态栏高度

3、应用区域(Activity)的宽高

Rect outRect = new Rect();

activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);w=outRect.rirht-outRect.left

h=outRect.bottom-outRect.top

ps:如果像妹子手机有smartBar、那么smartBar的高度=屏幕的高度-outRect.bottom(?guess)

注:在onCreate生命周期中有些尺寸或是无效的是无效的最好是在onWindowFocusChanged中获取

@SuppressLint("NewApi")

private void showMetres() {

int screenW,screenH;

int statusBarH;

int contentH;

int smartBarH;

//获取屏幕的宽高

DisplayMetrics outMetrics=new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(outMetrics);

screenW=outMetrics.widthPixels;

screenH=outMetrics.heightPixels;

//获取状态栏的高

Rect outRect=new Rect();

getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);

statusBarH=outRect.top;

//内容显示区的高

contentH=outRect.bottom-outRect.top;

//smartBarH=screenH-outRect.bottom;

Rect rect=new Rect();

getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);

int i=rect.bottom-rect.top;

smartBarH=screenH-rect.bottom;//这个可能不正确……!!!!

StringBuilder sb=new StringBuilder("屏幕的宽为:"+screenW+"\n")

.append("屏幕的高为:"+screenH+"\n")

.append("状态栏的高为:"+statusBarH+"\n")

.append("标题栏的高为:"+getActionBar().getHeight()+"\n")//标题栏的高

.append("内容显示区的高为(包含SmartBar):"+contentH+"\n")

.append("内容显示区的高为(不包含SmartBar):"+i+"\n")

.append("smartBar的高为:"+smartBarH);

TextView tv = (TextView) findViewById(R.id.tv);

tv.setText(sb.toString());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值