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

  • 本文关键字:方向 Nexus 获取 导航 android
  • 更新时间 :
  • 英文 :


在横向模式下,Android 7.1引入了当设备旋转时软按钮栏的重新定位。在7.1之前,无论你如何拿手机,它都会一直放在视野的右侧。

在我的应用程序中,我曾经通过软按钮条宽度来缩小(和移动(视图,如下所示:

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

但现在我需要知道酒吧是在右边还是左边。是的,当然我可以检查设备旋转和安卓版本,但我认为这种方法并不可靠。

有没有办法知道我的导航栏是位于当前视图的左侧还是右侧?

以下是我解决问题的方法。

处理方向变化:

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

最新更新