Android中是否有一种方法可以获取软按钮栏和状态栏的大小?还是要在没有这两个条的高度的情况下获得屏幕高度? (它们并不总是相同的,例如,查看Nexus 7)
状态栏的高度
状态栏的高度取决于屏幕尺寸,例如,对于具有320 X 480]的设备,状态屏幕高度为20px,例如在屏幕尺寸为[[240 X 320的设备中” >屏幕尺寸,状态栏高度为25px,对于具有480 x 800的设备,状态栏高度必须为38px所以我建议使用此脚本来获取状态栏的高度
Rect rectangle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;
Log.i("*** Value :: ", "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight);
要获取“活动”的onCreate()
方法上状态栏的高度,请使用此方法:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
软键按钮栏
此方法在设置Android KitKat(4.4)中的布局填充时非常有用。使用此功能,可以避免软按钮栏在布局上重叠。
getRealMetrics
方法仅适用于API 17和+
@SuppressLint("NewApi")
private int getSoftButtonsBarHeight() {
// getRealMetrics is only available with API 17 and +
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
return 0;
}
参考:
Height of status bar in Android