/**
* 获取状态栏高度
* 该方法获取需要在onWindowFocusChanged方法回调之后
*
* @param context
* @return
*/
public static int getStatusBarHeight(Context context) {
int statusBarHeight = getStatusBarByResId(context);
if (statusBarHeight <= 0) {
statusBarHeight = getStatusBarByReflex(context);
}
return statusBarHeight;
}
/**
* 通过状态栏资源id来获取状态栏高度
*
* @param context
* @return
*/
private static int getStatusBarByResId(Context context) {
int height = 0;
//获取状态栏资源id
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
try {
height = context.getResources().getDimensionPixelSize(resourceId);
} catch (Exception e) {
}
}
return height;
}
/**
* 通过Activity的内容距离顶部高度来获取状态栏高度,该方法获取需要在onWindowFocusChanged回调之后
*
* @param activity
* @return
*/
public static int getStatusBarByTop(Activity activity) {
Rect rect = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rect);
return rect.top;
}
/**
* 通过反射获取状态栏高度
*
* @param context
* @return
*/
private static int getStatusBarByReflex(Context context) {
int statusBarHeight = 0;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}
android获取状态栏高度
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 获取状态栏高度 //获取status_bar_height资源的ID intresourceId = getRes...
- 手机顶部显示时间、电量等信息的叫状态栏,即statusBar有些手机比如华为在底部会有返回、回到主页面等虚拟功能键...
- RSA前端生成公私钥对、私钥签名、公钥验签,前端打杂多年,第一次需要搞到这个鬼东西,一开始我内心有一百个拒绝拒绝拒...