获取Nexus (7.1.1)导航栏方向(左/右)

3
在横屏模式下,Android 7.1引入了软键盘栏在设备旋转时的重新定位。在7.1之前,无论你如何拿着手机,软键盘栏总是放置在视图的右侧。
在我的应用中,我曾经通过软键盘栏的宽度来缩小(和移动)视图,就像这样:
getGameActivity().getWindowManager().getDefaultDisplay().getRealMetrics(real);

但现在我需要知道导航栏是在视图的右侧还是左侧。

没错,我可以检查设备旋转和Android版本,但我认为这种方法并不真正可靠。

是否有任何方法可以知道我的导航栏位于当前视图的左侧还是右侧?

1个回答

3
这里是我解决问题的方法。
为了处理方向变化:
    android.view.OrientationEventListener mOrientationEventListener = new android.view.OrientationEventListener(this, android.hardware.SensorManager.SENSOR_DELAY_NORMAL)
    {
        @Override
        public void onOrientationChanged(int orientation) {

            if (orientation > 60 && orientation < 120) {
                orientation = 0;
            } else if (orientation > 240 && orientation < 300) {
                orientation = 1;
            } else {
                return;
            }

            if (prevOrientation != orientation) {
                Log.d("", "onOrientationChanged NEW " + orientation);

                prevOrientation = orientation;

                Handler handler = new Handler(Looper.getMainLooper());
                final Runnable r = new Runnable() {
                    public void run() {
                       // (notify your code that orientation changed)
                    }
                };
                handler.postDelayed(r, 1200);
            }
        }
    };

    if(mOrientationEventListener.canDetectOrientation()) {
        mOrientationEventListener.enable();
    }

然后,要检查导航栏是否在左侧:

    int rotation =  getGameActivity().getWindowManager().getDefaultDisplay().getRotation();
    int reqOr = getGameActivity().getRequestedOrientation();

    String aVerReleaseStr = Build.VERSION.RELEASE;
    int dotInd = aVerReleaseStr.indexOf(".");
    if (dotInd >= 0) {
        aVerReleaseStr = aVerReleaseStr.replaceAll("\\.", "");
        aVerReleaseStr = new StringBuffer(aVerReleaseStr).insert(dotInd, ".").toString();
    }

    float androidVer = Float.parseFloat(aVerReleaseStr);
    if (rotation == 3 && reqOr == 6 && androidVer >= 7.1) {
       // buttons are on the left side.
    }

不需要解析Android版本发布字符串,您可以直接查找Build.VERSION.SDK_INT属性。7.1是25个API级别。 - mrpasqal

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