gpt4 book ai didi

java - 获取状态栏的高度

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:27 25 4
gpt4 key购买 nike

我正在尝试编写一个 private 来获取 status bar 的高度。

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 :

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

第二

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

关于java - 获取状态栏的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20281331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com