Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
当前回答
上面的答案对某些人不起作用的原因是,在视图准备好渲染之前,您无法获得视图的维度。使用一个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
}
}
}
这是最可靠的方法。
其他回答
在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
切换全屏解决方案:
这个解决方案可能看起来像一个变通方法,但它实际上解释了你的应用程序是否全屏(也就是隐藏状态栏):
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point(); display.getSize(size);
int barheight = size.y - findViewById(R.id.rootView).getHeight();
这样,如果你的应用程序目前是全屏的,barheight将等于0。
就我个人而言,我不得不使用这个来纠正绝对TouchEvent坐标,以解释状态栏:
@Override
public boolean onTouch(View view,MotionEvent event) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point(); display.getSize(size);
int YCoord = (int)event.getRawY() - size.y + rootView.getHeight());
}
那会得到绝对y坐标无论应用是否全屏。
享受
在MDPI设备上,状态栏是25px。我们可以将其作为基础,并将其乘以密度(四舍五入)来获得任何设备上的状态栏高度:
int statusBarHeight = Math.ceil(25 * context.getResources().getDisplayMetrics().density);
参考参数:ldpi=。75, mdpi=1, hdpi=1.5, xhdpi=2
我有同样的问题,必须在onCreate中获得状态栏的高度。这对我很有用。
private static final int LOW_DPI_STATUS_BAR_HEIGHT = 19;
private static final int MEDIUM_DPI_STATUS_BAR_HEIGHT = 25;
private static final int HIGH_DPI_STATUS_BAR_HEIGHT = 38;
在onCreate内部:
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
int statusBarHeight;
switch (displayMetrics.densityDpi) {
case DisplayMetrics.DENSITY_HIGH:
statusBarHeight = HIGH_DPI_STATUS_BAR_HEIGHT;
break;
case DisplayMetrics.DENSITY_MEDIUM:
statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
break;
case DisplayMetrics.DENSITY_LOW:
statusBarHeight = LOW_DPI_STATUS_BAR_HEIGHT;
break;
default:
statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
}
See:
http://developer.android.com/reference/android/util/DisplayMetrics.html http://developer.android.com/guide/practices/ui_guidelines/icon_design.html
是的,当我尝试使用视图时,它提供了25px的结果。 以下是整个代码:
public class SpinActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout lySpin = new LinearLayout(this);
lySpin.setOrientation(LinearLayout.VERTICAL);
lySpin.post(new Runnable()
{
public void run()
{
Rect rect = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
int contentViewTop =
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight = contentViewTop - statusBarHeight;
System.out.println("TitleBarHeight: " + titleBarHeight
+ ", StatusBarHeight: " + statusBarHeight);
}
}
}
}