当设备上有软件底部导航按钮(Back,Home,Menu)时,我试图在屏幕(或屏幕上的元素)上添加底部填充。但是,看起来没有100%准确的方法来获得高度。
我尝试过的是:
var androidApp = Application.android;
var context = Utility.ad.getApplicationContext();
var resources = context.getResources();
var resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
console.log(resources.getDimensionPixelSize(resourceId))
}
但是这似乎总是返回一些值--126(在模拟器和带有可配置的开启/关闭软件按钮的真实设备上,否则都是硬件按钮))。由于SO的许多答案已经证实,这个解决方案不适用于HTC,OnePlus和其他一些设备。
接下来我想尝试的是这个答案 - How to get height and width of navigation bar programmatically。不幸的是,我正在努力将其转化为Nativescript。
答案:
通过将应用程序可用屏幕大小与实际屏幕大小进行比较,我获得了导航栏大小。我认为当应用程序可用的屏幕尺寸小于实际屏幕尺寸时,存在导航栏。然后我计算导航栏的大小。此方法适用于API 14及更高版本。
public static Point getNavigationBarSize(Context context) {
Point appUsableSize = getAppUsableScreenSize(context);
Point realScreenSize = getRealScreenSize(context);
// navigation bar on the right
if (appUsableSize.x < realScreenSize.x) {
return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
}
// navigation bar at the bottom
if (appUsableSize.y < realScreenSize.y) {
return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
}
// navigation bar is not present
return new Point();
}
public static Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
public static Point getRealScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else if (Build.VERSION.SDK_INT >= 14) {
try {
size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {} catch (NoSuchMethodException e) {}
}
return size;
}
我正在使用NS3,Angular 4并且对Java或Nativescript编程没有任何线索:)
关于如何实现此问题以及如何转换代码的任何想法(如果您认为这是最佳选择)?
答案 0 :(得分:0)
我在typescript中给出了代码:
var app = require("application");
declare var android:any;
export function getNavigationBarSize() {
let context = app.android.context;
let appUsableSize = getAppUsableScreenSize();
let realScreenSize = getRealScreenSize();
// navigation bar on the right
// I dont care about right nav bar, so i return 0
if (appUsableSize.x < realScreenSize.x) {
return 0; // new android.graphics.Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
}
// navigation bar at the bottom
if (appUsableSize.y < realScreenSize.y) {
return realScreenSize.y - appUsableSize.y; //new android.graphics.Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
}
// navigation bar is not present
return 0; //new android.graphics.Point();
}
export function getAppUsableScreenSize() {
let context = app.android.context;
let windowManager = context.getSystemService(android.content.Context.WINDOW_SERVICE);
let display = windowManager.getDefaultDisplay();
let size = new android.graphics.Point();
display.getSize(size);
return size;
}
export function getRealScreenSize() {
let context = app.android.context;
let windowManager = context.getSystemService(android.content.Context.WINDOW_SERVICE);
let display = windowManager.getDefaultDisplay();
let size = new android.graphics.Point();
if (android.os.Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else if (android.os.Build.VERSION.SDK_INT >= 14) {
try {
size.x = android.view.Display.class.getMethod("getRawWidth").invoke(display);
size.y = android.view.Display.class.getMethod("getRawHeight").invoke(display);
} catch (e) {
console.log(e);
console.log(e.stack);
}
}
return size;
}
然后我常常检查实际的显示高度:
import platform = require("tns-core-modules/platform");
let navHeight = getNavigationBarSize() / platform.screen.mainScreen.scale;
let usableHeight = platform.screen.mainScreen.heightDIPs - navHeight;