Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
当前回答
如果你知道它的大小和高度
like
例如,在屏幕尺寸为320 X 480的设备中,状态栏高度为25px,对于屏幕尺寸为480 X 800的设备,状态栏高度必须为38px
然后你可以得到视图的宽度/屏幕大小,你可以使用if else语句来获得状态栏的高度
其他回答
上面的答案对某些人不起作用的原因是,在视图准备好渲染之前,您无法获得视图的维度。使用一个OnGlobalLayoutListener来获得所说的维度,当你实际上可以:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= 16) {
decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
// Nice one, Google
decorView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
rect.top; // This is the height of the status bar
}
}
}
这是最可靠的方法。
在我用来获取状态栏高度的所有代码示例中,唯一一个在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;
}
显然,状态栏的实际高度被保留为Android资源。上面的代码可以添加到ContextWrapper类中(例如,一个Activity)。
网址:http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/
为了解决这个问题,我使用了一种组合方法。 这是必要的,因为在平板电脑上,当调用display.getHeight()时,系统栏已经减去了它的像素。 所以我首先检查系统栏是否存在,然后本克莱顿方法,这在手机上很有效。
public int getStatusBarHeight() {
int statusBarHeight = 0;
if (!hasOnScreenSystemBar()) {
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
}
return statusBarHeight;
}
private boolean hasOnScreenSystemBar() {
Display display = getWindowManager().getDefaultDisplay();
int rawDisplayHeight = 0;
try {
Method getRawHeight = Display.class.getMethod("getRawHeight");
rawDisplayHeight = (Integer) getRawHeight.invoke(display);
} catch (Exception ex) {
}
int UIRequestedHeight = display.getHeight();
return rawDisplayHeight - UIRequestedHeight > 0;
}
240x320 - 20px 320x480 - 25px 480x800+ - 38px
如果你知道它的大小和高度
like
例如,在屏幕尺寸为320 X 480的设备中,状态栏高度为25px,对于屏幕尺寸为480 X 800的设备,状态栏高度必须为38px
然后你可以得到视图的宽度/屏幕大小,你可以使用if else语句来获得状态栏的高度