それに応じてレイアウトを調整できるように、ロード時にAndroidナビゲーションバーが存在するかどうかを確認しようとしていますが、何か提案はありますか?
これは私が検出しようとしているナビゲーションバーです:
PS私がこれまでに見つけたのは、バーを試して削除するための「悪い」方法だけです。
それに応じてレイアウトを調整できるように、ロード時にAndroidナビゲーションバーが存在するかどうかを確認しようとしていますが、何か提案はありますか?
これは私が検出しようとしているナビゲーションバーです:
PS私がこれまでに見つけたのは、バーを試して削除するための「悪い」方法だけです。
別の解決策(私のクラスUtilsUISystemの一部)
public static boolean hasNavBar (Resources resources)
{
//Emulator
if (Build.FINGERPRINT.startsWith("generic"))
return true;
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && resources.getBoolean(id);
}
このコードをアクティビティの onCreate() メソッドに追加できます。
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
// TODO: The navigation bar is visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
} else {
// TODO: The navigation bar is NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
});
この方法は私のために働いた
int id = getResources().getIdentifier("config_showNavigationBar","bool","android");
boolean result = id > 0 && getResources().getBoolean(id);
//
if(result) {
// Do whatever you need to do, this device has a soft Navigation Bar
}
それは私にとってはうまくいき、多くのデバイスでテストされました。
boolean hasNavBar(Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// navigation bar was introduced in Android 4.0 (API level 14)
Resources resources = context.getResources();
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
return resources.getBoolean(id);
} else { // Check for keys
boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
return !hasMenuKey && !hasBackKey;
}
} else {
return false;
}
}
おそらくうまくいくはずのことは、画面を測定することです。
API 17 以降でgetWindowManager().getDefaultDisplay().getRealSize()
は、 によって返されるサイズと比較できる がありますgetWindowManager().getDefaultDisplay().getSize()
。
異なる結果が得られた場合はナビゲーション バーがあり、同じ結果が得られた場合はナビゲーション バーがないと言っても過言ではありません。注意すべきことの 1 つは、ターゲット SDK とサポートされている画面getSize()
です。Android がスケーリングなしでは現在のデバイスでアプリがうまく動作しないと判断した場合、結果がスケーリングされる可能性があります。
getWindowManager().getDefaultDisplay().getMetrics()
API 17 より下では、横向きモードと縦向きモードの両方で画面を測定できます。また、結果が異なる場合は、おそらくナビゲーション バーがあることを意味します。
ただし、同じ結果が得られたとしても、実際にはわかりません。携帯電話は、横向きの場合でもナビゲーション バーを短辺に保つことができるためです。経験に基づいた推測では、幅または高さのいずれかが 、 、 などの標準サイズよりも 4% から 8% 小さく1280x800
、1280x720
他1024x600
の寸法が等しい場合は、おそらくナビゲーション バーがあると考えられます。ただし、それに賭けないでください。これがうまく機能するには、解決策が多すぎますが、互いにほとんど違いがありません。
上記の回答を見て、「存在しない」を高さ 0 と見なすことができることを示したいと思います。したがって、次のようになります。
public static int getScreenH(Context context) {
DisplayMetrics dm = new DisplayMetrics();
dm = context.getResources().getDisplayMetrics();
int h = dm.heightPixels;
return h;
}
public static int getDpi(Context context) {
DisplayMetrics displayMetrics1 = context.getResources().getDisplayMetrics();
int height1 = displayMetrics1.heightPixels;
int dpi = 0;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
@SuppressWarnings("rawtypes")
Class c;
try {
c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked")
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, displayMetrics);
dpi = displayMetrics.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return dpi;
}
public static int getBottomStatusHeight(Context context) {
int totalHeight = getDpi(context);
int contentHeight = getScreenH(context);
return totalHeight - contentHeight;
}
```
解決策: 永続的なハードウェア キーを持たないデバイスにのみナビゲーション バーがあるため、API バージョンを確認し、hasPermanentMenuKey() を使用してハードウェア キーを見つけることができます。
boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();