繁体   English   中英

获得STATUS BAR的高度

[英]Getting the height of STATUS BAR

我想写一个private来获取status bar's高度。

private int statusBar() {
    Rect rect = new Rect();
    int statusBar = rect.top;
    return statusBar;
}

到目前为止一切都很好。 但是当我这样做时:

Toast.makeText(getApplicationContext(), "" + statusBar(), Toast.LENGTH_LONG).show();

它返回0。

刚刚搜索了网站一段时间,得到了这个。

private int statusBar() {
    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 statusBar = contentViewTop - StatusBarHeight;
    return statusBar;
}

祝酒词:

Toast.makeText(getApplicationContext(), "" + statusBar(), Toast.LENGTH_LONG).show();

看起来很有效。

更新:

尝试这个,如果没有工作尝试另一个。

private int statusBar() {
    Rect rectgle= new Rect();
    Window window= getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int statusBar = rectgle.top;
    return statusBar;
}

第2

private int statusBar() {
    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 statusBar = contentViewTop + StatusBarHeight;
    return statusBar;
}

尝试这个:

Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;

然后Toast statusBarHeight

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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