Joe*_*ery 5 java android galaxy
我正在尝试检查导航栏的可见性。在三星 Galaxy S8 上,我可以切换导航栏的可见性。
我尝试了很多不同的方法来检查可见性,但它们都不适用于 Galaxy S8。
一些例子:(它们将始终返回相同的值,无论是显示还是隐藏)
ViewConfiguration.get(getBaseContext()).hasPermanentMenuKey()总是返回false
KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)总是返回false
KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME)总是返回true
即使通过计算导航栏高度(如何以编程方式获取 Android 导航栏的高度和宽度?),它也不会起作用。
小智 1
也许这对某人有用。用户可以隐藏导航栏,因此检测可见性的最佳方法是订阅此事件。有用。
object NavigationBarUtils {
// Location of navigation bar
const val LOCATION_BOTTOM = 0
const val LOCATION_RIGHT = 1
const val LOCATION_LEFT = 2
const val LOCATION_NONE = 3
fun addLocationListener(activity: Activity, listener: (location: Int) -> Unit) {
ViewCompat.setOnApplyWindowInsetsListener(activity.window.decorView) { view, insets ->
val location = when {
insets.systemWindowInsetBottom != 0 -> LOCATION_BOTTOM
insets.systemWindowInsetRight != 0 -> LOCATION_RIGHT
insets.systemWindowInsetLeft != 0 -> LOCATION_LEFT
else -> LOCATION_NONE
}
listener(location)
ViewCompat.onApplyWindowInsets(view, insets)
}
}
}
Run Code Online (Sandbox Code Playgroud)