我正在尝试编写一个 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/