简体   繁体   中英

android get available height and width for portrait and landscape

so I have written the following method in my activity:

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

}

And it works great, in the sense that it gets me the total width and height of the screen with great accuracy and reliability (which is what I have asked it to do).

Here is the problem. On older android devices the screen dimensions are the same as the dimensions the application can take up, and the script above helps me to set the size of elements in the app. BUT with android ICS I have this graphic button bar on the bottom of the screen, and it messes up my whole strategy.

在此输入图像描述

What I would really like is the ability to get the available app dimensions for the portrait view as well as the landscape view at the same time in one method. And have these dimensions be accurate regardless of the presence of the bar pictured above.

Does anyone know how to achieve this?

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

From what I've read so far you can't get this height directly. The approach via getWindowVisibleDisplayFrame() does not seem to work. The only promising solution I have found so far is using a custom layout to measure the screen size as explained in http://evgeni-shafran.blogspot.de/2011/01/android-screen-size-problem.html . I am convinced that this will work but to me it seems to be more trouble than it is worth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM