显示指标减去状态栏? [英] Display metrics minus status bar?

查看:78
本文介绍了显示指标减去状态栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取应用程序可以在设备上使用的显示矩形的尺寸.为此,我尝试使用:

I need to get the dimensions of the display rectangle that the application can use on the device. For that I tried using:

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

我的问题是,它给了我整个显示屏的高度,并且显示屏顶部有一个状态/通知"栏,该栏无法应用程序使用.

My problem is that it gives me the height of the whole display and the display has a "status / notification" bar on top that the application can't use.

我需要应用程序可以使用的尺寸.

I need the acual dimension that the application can use.

为帮助您更好地理解问题,我会留下一张图片:

To help you understand the question better I'll leave an image:

推荐答案

所有这些的最大窍门是,在布局完成之前,通常无法访问该视图大小的真实值.这意味着onCreate()(通常也是onResume())在处理过程中还为时过早,无法进行计算.以下代码将为您提供一个表示Activity内容视图的视图,此时您可以检查其高度和宽度:

The biggest trick to all of this is that you can't usually gain access to a true value of that view's size until layout is complete. Which means onCreate() (and often onResume() also) are too early in the process to do the calculation. The following code will get you a view representing the content view of the Activity, at which point you can examine its height and width:

View content = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
Log.d("DISPLAY", content.getWidth() + " x " + content.getHeight());

这也说明您可能在Activity中设置的所有标题视图.当XML膨胀时,您还可以获得对设置为内容视图的根布局的引用;如果在两个维度上将该布局都设置为fill_parent,则可以执行相同的操作.

This also accounts for any title views you may have set in the Activity. You could also obtain a reference to the root layout you set as the content view when the XML is inflated and do the same thing if that layout is set to fill_parent in both dimensions.

我经常这样拨打电话的好方法是onWindowFocusChanged(),它将在您的活动"对用户几乎可见时被调用.

A good method where I often make calls like this is onWindowFocusChanged() which will be called at the point when your Activity is just about visible to the user.

希望有帮助!

这篇关于显示指标减去状态栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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