android获取纵向和横向的可用高度和宽度

Fre*_*all 6 java android android-layout

所以我在我的活动中写了以下方法:

private void setDisplayMetrics(){

    DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    int dh       = metrics.heightPixels;
    int dw       = metrics.widthPixels;

    if(dw < dh){
        deviceWidth  = dw;
        deviceHeight = dh;
    }else{
        deviceWidth  = dh;
        deviceHeight = dw;
    }

    System.err.println("--------------> dh : "+deviceHeight+" | dw "+deviceWidth);

}
Run Code Online (Sandbox Code Playgroud)

它的效果非常好,从某种意义上说,它可以让我获得屏幕的总宽度和高度,具有极高的准确性和可靠性(这就是我要求它做的事情).

这是问题所在.在较旧的Android设备上,屏幕尺寸与应用程序可以占用的尺寸相同,上面的脚本可以帮助我设置应用程序中元素的大小.但是使用android ICS我在屏幕底部有这个图形按钮栏,它会影响我的整个策略.

在此输入图像描述

我真正想要的是能够在一种方法中同时获得纵向视图和横向视图的可用应用维度.无论上图所示的条形图如何,这些尺寸都是准确的.

有谁知道如何实现这一目标?

San*_*p P 0

Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop= 
    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;

   Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight); 
Run Code Online (Sandbox Code Playgroud)

  • 我的 StatusBarHeight、contentViewTop 和 TitleBarHeight 为 0。 (2认同)