項目中用到了,網上找到一段代碼,能夠有效的獲取狀態欄的高度,記錄備忘。
[java]
/**
* 獲取狀態欄高度
*
* @return
*/
public int getStatusBarHeight()
{
Class<?> c = null;
Object obj = null;
java.lang.reflect.Field field = null;
int x = 0;
int statusBarHeight = 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());
statusBarHeight = getResources().getDimensionPixelSize(x);
return statusBarHeight;
}
catch (Exception e)
{
e.printStackTrace();
}
return statusBarHeight;
}
/**
* 獲取狀態欄高度
*
* @return
*/
public int getStatusBarHeight()
{
Class<?> c = null;
Object obj = null;
java.lang.reflect.Field field = null;
int x = 0;
int statusBarHeight = 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());
statusBarHeight = getResources().getDimensionPixelSize(x);
return statusBarHeight;
}
catch (Exception e)
{
e.printStackTrace();
}
return statusBarHeight;
}