Android 8 - How to know what side the Navigation Bar is when going from LANDSCAPE to REVERSE_LANDSCAPE?

牧云@^-^@ 提交于 2020-01-06 05:51:12

问题


In Android 8, when the phone goes to LANDSCAPE the navigation will be on the right side of the screen, and when its in REVERSE_LANDSCAPE, it'll be on the left side. What I did to find out which side of the screen it is on was to have the root View of my activity and check it's size and position in these ways:

if (rootView.width == deviceScreenWidth) {
    // device is tablet and the navigation bar is at the bottom, or
    // its a samsung device with no navigation bar
} 
else if (rootView.positionOnScreenX == 0) {
    // the navigation bar is at the LEFT side
} 
else {
    // the navigation bar is at the RIGHT side
}

But I'm facing a new problem regarding this approach, which is if user goes directly from LANDSCAPE to REVERSE_LANDSCAPE navigation bar will change location, but the code doesn't run again which causes problems.

Is there any callbacks in android sdk to listen to this rotation? Or is there a better way to know which side of the screen the navigation bar is, without running into this problem?


回答1:


You can override Activity#onConfigurationChanged(Configuration newConfig) to get a callback when the user rotates their device.

You can checkout these links for more details:

  • https://developer.android.com/guide/topics/resources/runtime-changes

  • https://developer.android.com/reference/android/app/Activity#onConfigurationChanged(android.content.res.Configuration)



来源:https://stackoverflow.com/questions/50251727/android-8-how-to-know-what-side-the-navigation-bar-is-when-going-from-landscap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!