弄清楚状态栏是在顶部还是底部?

hwr*_*kns 8 android

我试图弄清楚如何分辨状态栏的位置(顶部或底部).我试图检查,HierarchyViewer但没有看到状态栏视图.

实际上我需要弄清楚的是,给定一个上下文,一种返回a的方法boolean(如果条形图在顶部则为true,如果不在条形区域则为false - 就像它不在大多数平板电脑上一样).我写了一个简单的解决方案,试图找出状态栏位于顶部或底部,但它似乎没有帮助:

private boolean isStatusBarAtTop(){
    if (!(getContext() instanceof Activity)) {
        return !getContext().getResources().getBoolean(R.bool.isTablet);
    }

    Window window =  ((Activity) getContext()).getWindow();

    if(window == null) {
        return !getContext().getResources().getBoolean(R.bool.isTablet);
    }

    Activity activity = (Activity)getContext();
    Rect rect = new Rect();

    window.getDecorView().getWindowVisibleDisplayFrame(rect);
    View ourView = window.findViewById(Window.ID_ANDROID_CONTENT);

    Log.d("Menu","Window Top: "+ ourView.getTop() + ", "+ourView.getBottom()+ ", "+ourView.getLeft()+", "+ourView.getRight());
    Log.d("Menu","Decor View Dimensions"+rect.flattenToString());

    return  ourView.getTop() != 0;
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,我得到以下输出(在Nexus 7平板电脑上运行):

D/Menu(1007): Window Top: 0, 0, 0, 0
D/Menu(1007): Decor View Dimensions0 0 800 1216
Run Code Online (Sandbox Code Playgroud)

我在想什么/做错了什么?

dro*_*_91 5

这是唯一对我有用的方法.以下方法返回高度的的顶部状态栏.因此,如果相应的返回值等于0,则窗口顶部没有状态栏.

public static int getTopStatusBarHeight(Activity activity) {
    Rect rect = new Rect();

    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

    return rect.top;
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,半透明状态栏将使其返回0. (3认同)

hwr*_*kns 1

实际上没有办法告诉状态栏的方向/位置。解释它的最好方法是确保你的观点始终解释它。

@Override
protected boolean fitSystemWindows(Rect insets) {
    RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)this.getLayoutParams());
    int bottom = params.bottomMargin;
    int left = params.leftMargin;
    int right = params.rightMargin;
    params.setMargins(left, insets.top, right, bottom);
    return super.fitSystemWindows(insets);
}
Run Code Online (Sandbox Code Playgroud)

此方法(您在 ViewGroup 中重写)背后的方法是inset从框架接收一个矩形,然后将这些插图添加到视图填充中。