我想在设备屏幕上显示导航栏上方的视图,否则显示在屏幕底部。
答案 0 :(得分:2)
在您的活动或片段中使用此方法,以了解设备是否具有软导航栏。 然后,您可以根据方法调用中的要求执行代码。
像这样: -
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);
}
答案 1 :(得分:0)
您可以尝试以下方法。它可以很好地工作:
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;
}