Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
当前回答
Kotlin版本,结合了两个最佳解决方案
fun getStatusBarHeight(): Int {
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
return if (resourceId > 0) resources.getDimensionPixelSize(resourceId)
else Rect().apply { window.decorView.getWindowVisibleDisplayFrame(this) }.top
}
如果存在,则接受status_bar_height值 如果status_bar_height不存在,则从Window decor计算状态栏高度
其他回答
试试这个:
Rect rect = new Rect();
Window win = this.getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight = contentViewTop - statusBarHeight;
Log.d("ID-ANDROID-CONTENT", "titleBarHeight = " + titleBarHeight );
它没有为我工作在onCreate方法的活动,但当我把它放在onClickListener,并给我一个25的测量
在android6.0中,状态栏的高度为24dp
<!-- Height of the status bar -->
<dimen name="status_bar_height">24dp</dimen>
<!-- Height of the bottom navigation / system bar. -->
<dimen name="navigation_bar_height">48dp</dimen>
你可以在源代码中找到答案:frameworks\base\core\res\res\values\ dimensions .xml
Kotlin版本,结合了两个最佳解决方案
fun getStatusBarHeight(): Int {
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
return if (resourceId > 0) resources.getDimensionPixelSize(resourceId)
else Rect().apply { window.decorView.getWindowVisibleDisplayFrame(this) }.top
}
如果存在,则接受status_bar_height值 如果status_bar_height不存在,则从Window decor计算状态栏高度
上面的答案对某些人不起作用的原因是,在视图准备好渲染之前,您无法获得视图的维度。使用一个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
}
}
}
这是最可靠的方法。
这个问题在… 状态栏的高度?
更新::
当前的方法:
好的,状态栏的高度取决于屏幕的大小,例如在一个设备中 对于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("*** Elenasys :: ", "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;
}