好久沒有總結了,寫些常用的方法方便我以後開發使用。好啦,不哈拉了,正文如下:
/**
* @Description: 獲取狀態欄的高度
* @Author:dream
* @Since:2015-12-17
* @Version:1.1
* @param context 當前上下文
* @return int 狀態欄高度
*/
public int getStatusBarHeight(Context context) {
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, statusBar = 0;
try {
//通過反射機制拿到狀態欄高度
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBar =context. getResources().getDimensionPixelSize(x);
return statusBar ;
} catch (Exception e1) {
Log.e("error","get statusBar height fail!!!");
e1.printStackTrace();
return 0;
}
}
/**
* @Description:返回標題欄的高度
* @Author:dream
* @Since:2015-12-17
* @Version:1.1
* @param activity 當前上下文
* @return int 返回標題欄高度
*/
public int getTitleBarHeight(Activity activity){
//狀態欄和標題欄的總和
int contentTop = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是狀態欄的高度
//標題欄高度 = 總和 - 狀態欄高度
return contentTop - getStatusBarHeight(activity);
}
Android 獲取標題欄以及狀態欄的高度
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.