检测导航栏是否位于屏幕右侧

时间:2014-02-08 23:03:34

标签: android

我只需要检测导航栏是否位于屏幕右侧,如下图所示。感谢

screenshot with navbar to the right

3 个答案:

答案 0 :(得分:2)

以下代码段可以提供帮助:

private boolean isNavigationBarRightOfContent(){
    Rect outRect = new Rect();
    ViewGroup decor = (ViewGroup) mActivity.getWindow().getDecorView();
    decor.getWindowVisibleDisplayFrame(outRect);
    DisplayMetrics dm = getResources().getDisplayMetrics();
    return dm.widthPixels == outRect.bottom;
}

答案 1 :(得分:1)

如果导航栏处于横向模式,它将仅位于屏幕右侧。因此要检测到这一点,请使用getResources().getConfiguration().orientation,如下所示:

String orientation = getResources().getConfiguration().orientation;
if(orientation.equals("ORIENTATION_LANDSCAPE"){
    // screen in landscape, do what you want to do
}

答案 2 :(得分:0)

试试这个:

    // retrieve the position of the DecorView
    Rect visibleFrame = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(visibleFrame);

    DisplayMetrics dm = getResources().getDisplayMetrics();
    // check if the DecorView takes the whole screen vertically or horizontally
    boolean isRightOfContent = dm.heightPixels == visibleFrame.bottom;
    boolean isBelowContent   = dm.widthPixels  == visibleFrame.right;