Android中狀態欄的高度

本文翻譯自:Height of status bar in Android

What's the height of the status bar in Android? Android中狀態欄的高度是多少? Is it always the same? 總是一樣的嗎?

From my measurements it seems that it's 25dp, but I'm not sure if it has the same height on all platforms. 根據我的測量結果,它似乎是25dp,但我不確定它是否在所有平臺上具有相同的高度。

(I want to know this to properly implement a fade transition from an activity that doesn't have status bar to one that does) (我想知道這是爲了正確實現從沒有狀態欄的活動到執行狀態欄的活動的淡入淡出過渡)


#1樓

參考:https://stackoom.com/question/EINk/Android中狀態欄的高度


#2樓

Out of all the code samples I've used to get the height of the status bar, the only one that actually appears to work in the onCreate method of an Activity is this: 在我用來獲取狀態欄高度的所有代碼示例中,實際看起來在ActivityonCreate方法中工作的唯一一個是:

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

Apparently the actual height of the status bar is kept as an Android resource. 顯然,狀態欄的實際高度保留爲Android資源。 The above code can be added to a ContextWrapper class (eg an Activity ). 上面的代碼可以添加到ContextWrapper類(例如Activity )。

Found at http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/ 發現於http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/


#3樓

this also work with the refrence link 這也適用於參考鏈接

public int getStatusBarHeight() {
  int result = 0;
  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId > 0) {
      result = getResources().getDimensionPixelSize(resourceId);
  }
  return result;
}

#4樓

To solve this, I used a combination approach. 爲了解決這個問題,我使用了一種組合方法。 This is necessary as on tablets the system bar already subtracts it's pixels when display.getHeight() is called. 這是必要的,因爲在平板電腦上系統欄已經在調用display.getHeight()時減去它的像素。 So I first check if a system bar is present, and then Ben Claytons approach, which works fine on phones. 所以我首先檢查是否存在系統欄,然後Ben Claytons接近,這在手機上運行良好。

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;
}

#5樓

I've merged some solutions together: 我將一些解決方案合併在一起:

public static int getStatusBarHeight(final Context context) {
    final Resources resources = context.getResources();
    final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0)
        return resources.getDimensionPixelSize(resourceId);
    else
        return (int) Math.ceil((VERSION.SDK_INT >= VERSION_CODES.M ? 24 : 25) * resources.getDisplayMetrics().density);
    }

another alternative: 另一種選擇:

    final View view = findViewById(android.R.id.content);
    runJustBeforeBeingDrawn(view, new Runnable() {
        @Override
        public void run() {
            int statusBarHeight = getResources().getDisplayMetrics().heightPixels - view.getMeasuredHeight();
        }
    });

EDIT: Alternative to runJustBeforeBeingDrawn: https://stackoverflow.com/a/28136027/878126 編輯:runJustBeforeBeingDrawn的替代方案: https ://stackoverflow.com/a/28136027/878126


#6樓

Toggled Fullscreen Solution: 切換全屏解決方案:

This solution may look like a workaround, but it actually accounts for whether your app is fullscreen (aka hiding the status bar) or not: 此解決方案可能看起來像一種解決方法,但它實際上會說明您的應用是否是全屏(也就是隱藏狀態欄):

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point(); display.getSize(size);
int barheight = size.y - findViewById(R.id.rootView).getHeight();

This way, if your app is currently fullscreen, barheight will equal 0. 這樣,如果您的應用當前是全屏,則barheight將等於0。

Personally I had to use this to correct absolute TouchEvent coordinates to account for the status bar as so: 就個人而言,我必須使用它來糾正絕對的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());
}

And that will get the absolute y-coordinate whether the app be fullscreen or not. 無論應用程序是否全屏,這都將獲得絕對的y座標。

Enjoy 請享用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章