獲取狀態欄高度,兩個寫法
同事寫的:
private static boolean USE_CUSTOM_STATUS_BAR_HEIGHT = true;
private static int statusBarHeight = -1;
public static int getStatusBarHeight(Activity activity) {
if (statusBarHeight != -1 && !USE_CUSTOM_STATUS_BAR_HEIGHT){
return statusBarHeight;
}
try {
if (activity != null && !activity.isFinishing()){
Rect rect = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rect);
statusBarHeight = rect.top;
USE_CUSTOM_STATUS_BAR_HEIGHT = false;
}
} catch (Exception e){
LOGGER.d(TAG,"getStatusBarHeight Exception",e);
}
if (USE_CUSTOM_STATUS_BAR_HEIGHT){
statusBarHeight = DeviceUtils.fromDipToPx(ApplicationContext, 20);
}
return statusBarHeight;
}
我寫的:
private static int statusBarHeight = DeviceUtils.fromDipToPx(ApplicationContext, 20);
public static int getStatusBarHeight(Activity activity) {
if (activity == null||activity.isFinishing) {
return statusBarHeight;
} else {
Rect rectgle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
statusBarHeight = rectgle.top;
return rectgle.top;
}
}
從取值的角度講,這兩種獲取狀態欄高度的寫法有區別嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/58697.html
標籤:Android
