如何获取状态栏和软键按钮栏的高度?
How Get height of the Status Bar and Soft Key Buttons Bar?
在Android中有没有办法让软按钮栏和状态栏的大小一致?还是一种在没有这两个条的高度的情况下获取屏幕高度的方法? (它们并不总是相同的,以 nexus 7 为例)
Height of the Status Bar
状态栏的高度取决于屏幕尺寸,例如在 240 X 320 屏幕尺寸的设备中,状态栏高度为 20px,对于 320 X 480 屏幕尺寸的设备,状态栏高度为 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);
要在 Activity 的 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;
}
Soft Key Buttons Bar
为了在 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
Dimension of soft buttons bar
对我来说,在android注释10中,我需要通过dimen获取导航栏高度:
fun getSoftNavigationBarSize(resources: Resources): Int {
var result = 0
val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android")
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId)
}
return result
}
我知道这个问题已经得到解答,但我只是发现这个解决方案似乎比其他解决方案更容易。我只对底部按钮栏进行了尝试。不过,我很确定状态栏的工作方式相同。
view.rootWindowInsets?.let { inset ->
Log.e("XXX", "button bar height: ${inset.systemWindowInsetBottom}")
}
注意:此时您需要一个附加到 window 的视图。
private void getStatusAndNavigationBarsHeight() {
float deviceDensity = getResources().getDisplayMetrics().density;
float statusHeight = 0;
float navigationHeight = 0;
Resources myResources = getApplicationContext().getResources();
int idStatusBarHeight = myResources.getIdentifier( "status_bar_height", "dimen", "android");
statusHeight = idStatusBarHeight > 0 ? getResources().getDimensionPixelSize(idStatusBarHeight)/deviceDensity : 0;
statusHeight = Math.round(statusHeight);
Log.d("StatusBar Height(dp):", String.valueOf(statusHeight));
int idNavigationBarHeight = myResources.getIdentifier("navigation_bar_height", "dimen", "android");
navigationHeight = idNavigationBarHeight > 0 ? getResources().getDimensionPixelSize(idNavigationBarHeight)/deviceDensity : 0;
navigationHeight = Math.round(navigationHeight);
Log.d("Navigation Bar Height(dp):", String.valueOf(navigationHeight));
}
在Android中有没有办法让软按钮栏和状态栏的大小一致?还是一种在没有这两个条的高度的情况下获取屏幕高度的方法? (它们并不总是相同的,以 nexus 7 为例)
Height of the Status Bar
状态栏的高度取决于屏幕尺寸,例如在 240 X 320 屏幕尺寸的设备中,状态栏高度为 20px,对于 320 X 480 屏幕尺寸的设备,状态栏高度为 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);
要在 Activity 的 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;
}
Soft Key Buttons Bar
为了在 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
Dimension of soft buttons bar
对我来说,在android注释10中,我需要通过dimen获取导航栏高度:
fun getSoftNavigationBarSize(resources: Resources): Int {
var result = 0
val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android")
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId)
}
return result
}
我知道这个问题已经得到解答,但我只是发现这个解决方案似乎比其他解决方案更容易。我只对底部按钮栏进行了尝试。不过,我很确定状态栏的工作方式相同。
view.rootWindowInsets?.let { inset ->
Log.e("XXX", "button bar height: ${inset.systemWindowInsetBottom}")
}
注意:此时您需要一个附加到 window 的视图。
private void getStatusAndNavigationBarsHeight() {
float deviceDensity = getResources().getDisplayMetrics().density;
float statusHeight = 0;
float navigationHeight = 0;
Resources myResources = getApplicationContext().getResources();
int idStatusBarHeight = myResources.getIdentifier( "status_bar_height", "dimen", "android");
statusHeight = idStatusBarHeight > 0 ? getResources().getDimensionPixelSize(idStatusBarHeight)/deviceDensity : 0;
statusHeight = Math.round(statusHeight);
Log.d("StatusBar Height(dp):", String.valueOf(statusHeight));
int idNavigationBarHeight = myResources.getIdentifier("navigation_bar_height", "dimen", "android");
navigationHeight = idNavigationBarHeight > 0 ? getResources().getDimensionPixelSize(idNavigationBarHeight)/deviceDensity : 0;
navigationHeight = Math.round(navigationHeight);
Log.d("Navigation Bar Height(dp):", String.valueOf(navigationHeight));
}