1.在属性中可以这样设置更改ActionBar的高度
android:layout_marginTop="?android:attr/actionBarSize"
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.i(Tag,"statusBarHeight "+statusBarHeight); ///statusBarHeight = 0;
2.如果上面的方法得不到可以用下面反射来获取
在源码程序中获取状态栏高度代码: height= getResources().getDimensionPixelSize(com.Android.internal.R.dimen.status_bar_height);
我们这样写的:
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
int y = getResources().getDimensionPixelSize(x);
viewPager.setPadding(0,y,0,0); //完美获取状态栏的高度
} catch (Exception e) {
e.printStackTrace();
}