Android常用工具方法
本文意在整理安卓开发中常用的方法,处理一些需要特殊处理的地方
获取屏幕尺寸
屏幕尺寸DisplayMetrics 其中包含了很多信息
DisplayMetrics
{
int widthPixels
int heightPixels
float density
int densityDpi
float scaledDensity
int xdpi
int ydpi
}
public static DisplayMetrics
(Context context
) {
return context
.getResources().getDisplayMetrics();
}
获取某个属性资源文件id
public static int getIdentifier(Context context
,String sourceName
,String sourceType
,
String packageBelong
) {
return context
.getResources().getIdentifier(sourceName
, sourceType
, packageBelong
);
}
获取状态栏高度
public static int getStatusBarHeight(Context context
) {
return context
.getResources().getDimensionPixelSize(getIdentifier(context
,
"status_bar_height", "dimen", "android"));
}
获取底部导航栏高度
public static int getNavigationBarHeight(Context context
) {
return context
.getResources().getDimensionPixelSize(getIdentifier(context
,
"navigation_bar_height", "dimen", "android"));
}
判断是否有导航栏
public 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
;
}
转载请注明原文地址:https://tech.qufami.com/read-5548.html