Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
当前回答
为了解决这个问题,我使用了一种组合方法。 这是必要的,因为在平板电脑上,当调用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;
}
其他回答
在我用来获取状态栏高度的所有代码示例中,唯一一个在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/
硬编码大小或使用反射来获取status_bar_height的值被认为是不好的做法。Chris Banes在纽约Droidcon上讲过这个。获取状态栏大小的推荐方法是通过OnApplyWindowInsetsListener:
myView.setOnApplyWindowInsetsListener { view, insets -> {
val statusBarSize = insets.systemWindowInsetTop
return insets
}
这是在API 20中添加的,也可以通过ViewAppCompat进行反向移植。
感谢@Niklas +1,这是正确的方法。
public class MyActivity extends Activity implements android.support.v4.View.OnApplyWindowInsetsListener {
Rect windowInsets;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
View rootview = findViewById(android.R.id.content);
android.support.v4.View.ViewCompat.setOnApplyWindowInsetsListener(rootview, this);
}
android.support.v4.View.WindowInsetsCompat android.support.v4.View.OnApplyWindowInsetsListener.OnApplyWindowInsets(View v, android.support.v4.View.WindowInsetsCompat insets)
{
windowInsets = new Rect();
windowInsets.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(), insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
//StatusBarHeight = insets.getSystemWindowInsetTop();
//Refresh/Adjust view accordingly
return insets;
}
}
请原谅,如果代码不是100%正确,从Xamarin c#转换,但这是它的公正。工作与缺口等。
这个问题在… 状态栏的高度?
更新::
当前的方法:
好的,状态栏的高度取决于屏幕的大小,例如在一个设备中 对于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;
}
在Android 4.1及更高版本上,你可以将应用程序的内容设置为显示在状态栏后面,这样当状态栏隐藏或显示时,内容不会调整大小。为此,使用SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN。你可能还需要使用SYSTEM_UI_FLAG_LAYOUT_STABLE来帮助你的应用保持一个稳定的布局。
当您使用这种方法时,您有责任确保应用程序UI的关键部分(例如,地图应用程序中的内置控件)最终不会被系统栏覆盖。这可能会使你的应用程序无法使用。在大多数情况下,你可以通过添加android:fitsSystemWindows属性到你的XML布局文件,设置为true来处理这个问题。这将调整父ViewGroup的填充,为系统窗口留出空间。这对于大多数应用程序来说已经足够了。
In some cases, however, you may need to modify the default padding to get the desired layout for your app. To directly manipulate how your content lays out relative to the system bars (which occupy a space known as the window's "content insets"), override fitSystemWindows(Rect insets). The fitSystemWindows() method is called by the view hierarchy when the content insets for a window have changed, to allow the window to adjust its content accordingly. By overriding this method you can handle the insets (and hence your app's layout) however you want.
形式: https://developer.android.com/training/system-ui/status.html#behind