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

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

可能是检测Android导航栏方向的重复问题。 - NPike
请问如何更改导航栏的当前方向位置? - Benny
4个回答

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

2
你可以使用Insets API并注册OnApplyWindowInsetsListener来检查系统导航栏的高度,以了解方向的方向。
ViewCompat.setOnApplyWindowInsetsListener(
    window.decorView 
) { _: View, windowInsets: WindowInsetsCompat ->

    val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())

    if (insets.bottom > 0) {
        // Navigation Bar at Bottom

    } else if (insets.right > 0) {
        // Navigation Bar at Right

    } else if (insets.left > 0) {
        // Navigation Bar at Left

    }

    WindowInsetsCompat.CONSUMED

}

1
尝试一下:

Try this:

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

1
如果导航栏处于横屏模式下,它将只会位于屏幕的右侧。因此,要检测这一点,请像下面这样使用getResources().getConfiguration().orientation
String orientation = getResources().getConfiguration().orientation;
if(orientation.equals("ORIENTATION_LANDSCAPE"){
    // screen in landscape, do what you want to do
}

谢谢你的回答,但我已经知道了。问题是在某些设备上(大多数情况下),导航栏不在右边,而是在底部。 - Michael Kern
1
@ImmaWake 你为什么需要检测这个(只是好奇)?至于如何检测它...我不确定。 - hichris123
我几周前发布了这个问题>https://dev59.com/qnvaa4cB1Zd3GeqPAC0P,但没有得到有用的回答,所以我的解决方法是在我的布局顶部和底部添加与状态栏和导航栏宽度相匹配的填充。 - Michael Kern
这个说法不再总是正确。如果正在使用新的手势导航,则(非常细的)导航栏始终出现在屏幕底部。 - Matt Robertson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接