0
我想在導航欄上方顯示一個視圖,如果它存在於設備屏幕上,否則如果沒有,則顯示在屏幕底部。如何以編程方式知道android設備是否具有軟導航鍵?
我想在導航欄上方顯示一個視圖,如果它存在於設備屏幕上,否則如果沒有,則顯示在屏幕底部。如何以編程方式知道android設備是否具有軟導航鍵?
在您的活動或片段中使用此方法可知道設備是否具有軟導航欄。 然後,您可以根據方法調用中的要求來執行代碼。
像這樣: -
if (hasNavBar(getResources()))
{
//do your code here
}
public boolean hasNavBar (Resources resources)
{
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && resources.getBoolean(id);
}
你可以試試下面的方法。它可以很好地工作:
public boolean hasNavBar (Resources resources) {
boolean hasNavigationBar = true;
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasNavigationBar = resources.getBoolean(id);
}
try {
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method m = systemPropertiesClass.getMethod("get", String.class);
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
hasNavigationBar = false;
} else if ("0".equals(navBarOverride)) {
hasNavigationBar = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return hasNavigationBar;
}
請DONOT使問題差,要對它進行適當解釋 –
雖然問題應改寫,其他的答案只有通過與屏幕高度使Calcs(計算)給予workarround,應該有成爲更清潔的解決方案http://stackoverflow.com/questions/28406506/check-if-android-device-has-software-or-hardware-navigation-buttons – Nanoc