狀態(tài)欄即statusBar装盯,導航欄即某些手機底部有返回鍵的虛擬鍵那一欄,叫navigationBar甲馋。
1. 獲取頂部statusBar高度
private int getStatusBarHeight() {
Resources resources = mActivity.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen","android");
int height = resources.getDimensionPixelSize(resourceId);
Log.v("dbw", "Status height:" + height);
return height;
}
2. 獲取底部navigationBar高度
private int getNavigationBarHeight() {
Resources resources = mActivity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
int height = resources.getDimensionPixelSize(resourceId);
Log.v("dbw", "Navi height:" + height);
return height;
}
3. 獲取設備是否存在NavigationBar
//獲取是否存在NavigationBar
public static boolean checkDeviceHasNavigationBar(Context context) {
boolean hasNavigationBar = false;
Resources rs = context.getResources();
int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasNavigationBar = rs.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) {
}
return hasNavigationBar;
}
4. 在頁面未加載出來前埂奈,獲取控件的寬高
view.post(new Runnable() {
@Override
public void run() {
int viewWidth = view.getWidth();//view寬度
int viewHeight = view.getHeight();//view高度
}
});
5. 獲取屏幕寬度
在頁面未加載出來前,直接通過getWidth()
獲取的寬度為0定躏,即使你在onResume()
方法中執(zhí)行這個方法也是一樣账磺。
public static int getScreenWidth(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
return dm.widthPixels;
}
6. 獲取屏幕高度
public static int getScreenHeight(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
return dm.heightPixels;
}