Android 某些设备上的不可见软导航栏
我想知道设备是否有软导航条。为此,我使用以下代码:Android 某些设备上的不可见软导航栏,android,button,kotlin,softkeys,Android,Button,Kotlin,Softkeys,我想知道设备是否有软导航条。为此,我使用以下代码: val hasNavBar = resources.getIdentifier("config_showNavigationBar", "bool", "android") if(hasNavBar > 0 && resources.getBoolean(hasNavBar)){ val resourceId = resources.getIdentifier("n
val hasNavBar = resources.getIdentifier("config_showNavigationBar", "bool", "android")
if(hasNavBar > 0 && resources.getBoolean(hasNavBar)){
val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
if (resourceId > 0) {
ApiHelper.navHeight = resources.getDimensionPixelSize(resourceId)
}
}
但是在一些没有软导航栏的设备上,resources.getDimensionPixelSize(resourceId)
提供了一些高度,并且hasNavBar
是正确的
出现此问题的设备是Redmi Note 4 Pro。我不知道其他设备是否也有同样的问题
我找到了答案
stackOverflow中有一些函数不起作用,但它起作用了
快捷键(windowManager:windowManager):布尔值{
var hasSoftwareKeys=true
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.JELLY\u BEAN\u MR1){
val d=context.getWindowManager().getDefaultDisplay()
val realDisplayMetrics=DisplayMetrics()
d、 getRealMetrics(realDisplayMetrics)
val realHeight=realDisplayMetrics.heightPixels
val realWidth=realDisplayMetrics.widthPixels
val displayMetrics=displayMetrics()
d、 getMetrics(displayMetrics)
val displayHeight=displayMetrics.heightPixels
val displayWidth=displayMetrics.widthPixels
hasSoftwareKeys=realWidth-displayWidth>0 | | realHeight-displayHeight>0x<
}否则{
val hasMenuKey=ViewConfiguration.get(context.hasPermanentMenuKey)()
val hasBackKey=KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE\u BACK)
hasSoftwareKeys=!hasMenuKey&&!hasBackKey
}
返回hasSoftwareKeys
}
fun hasSoftKeys(windowManager: WindowManager): Boolean {
var hasSoftwareKeys = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
val d = context.getWindowManager().getDefaultDisplay()
val realDisplayMetrics = DisplayMetrics()
d.getRealMetrics(realDisplayMetrics)
val realHeight = realDisplayMetrics.heightPixels
val realWidth = realDisplayMetrics.widthPixels
val displayMetrics = DisplayMetrics()
d.getMetrics(displayMetrics)
val displayHeight = displayMetrics.heightPixels
val displayWidth = displayMetrics.widthPixels
hasSoftwareKeys = realWidth - displayWidth > 0 || realHeight - displayHeight > 0x<
} else {
val hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey()
val hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)
hasSoftwareKeys = !hasMenuKey && !hasBackKey
}
return hasSoftwareKeys
}