Android 8-如何知道从LANDSCAPE到REVERSE_LANDSCAPE时导航栏的哪一侧? [英] Android 8 - How to know what side the Navigation Bar is when going from LANDSCAPE to REVERSE_LANDSCAPE?

查看:119
本文介绍了Android 8-如何知道从LANDSCAPE到REVERSE_LANDSCAPE时导航栏的哪一侧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android 8中,当手机转到LANDSCAPE时,导航将在屏幕的右侧,而在REVERSE_LANDSCAPE中,导航将在屏幕的左侧. 我要做的就是找出屏幕的哪一侧是活动的根View,并通过以下方式检查其大小和位置:

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
}

但是关于这种方法,我遇到了一个新问题,即如果用户直接从LANDSCAPE转到REVERSE_LANDSCAPE,导航栏将更改位置,但是代码不会再次运行,这会导致问题.

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.

android sdk中是否有任何回调来侦听此轮换?还是有更好的方法来知道导航栏在屏幕的哪一侧,而不会遇到此问题?

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?

推荐答案

当用户旋转设备时,您可以覆盖Activity#onConfigurationChanged(Configuration newConfig)以获取回调.

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/reference/android/app/Activity#onConfigurationChanged(android.content.res.Configuration)

这篇关于Android 8-如何知道从LANDSCAPE到REVERSE_LANDSCAPE时导航栏的哪一侧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆