【问题标题】:android get available height and width for portrait and landscapeandroid获取纵向和横向的可用高度和宽度
【发布时间】:2012-10-19 09:50:14
【问题描述】:

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

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

}

而且它工作得很好,因为它以极高的准确性和可靠性让我获得了屏幕的总宽度和高度(这是我要求它做的)。

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

我真正想要的是能够以一种方法同时获取纵向视图和横向视图的可用应用尺寸。并且无论是否存在上图中的条,这些尺寸都必须准确。

有谁知道如何做到这一点?

【问题讨论】:

标签: java android android-layout


【解决方案1】:
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); 

【讨论】:

  • 状态栏不就是顶部的栏吗?
  • StatusBarHeight、contentViewTop 和 TitleBarHeight 的值为 0。
【解决方案2】:

根据我目前所读到的内容,您无法直接得到这个高度。通过 getWindowVisibleDisplayFrame() 的方法似乎不起作用。 到目前为止,我发现的唯一有希望的解决方案是使用自定义布局来测量屏幕尺寸,如 http://evgeni-shafran.blogspot.de/2011/01/android-screen-size-problem.html。 我相信这会奏效,但对我来说,这似乎比它的价值更麻烦。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 2018-10-07
    • 2014-07-01
    • 1970-01-01
    相关资源
    最近更新 更多