在横向模式下,android 7.1在设备旋转时引入了软按钮条的重新定位。
在7.1之前,它总是放在视图的右侧,无论您如何握着手机。
在我的应用程序中,我曾经按软按钮条宽度缩小(和移动)视图,如下所示:

getGameActivity().getWindowManager().getDefaultDisplay().getRealMetrics(real);

但现在我需要知道酒吧是在右边还是左边。
是的,当然我可以检查设备的旋转和android版本,但我认为这种方法并不真正可靠。
有没有办法知道我的导航栏是位于当前视图的左侧还是右侧?

最佳答案

这就是我解决问题的方法。
要处理方向更改:

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

09-10 07:15
查看更多