当前位置:首页 > 青鸟知识 > android

Android取得statusBar高度

来源:湖南新途教育 发布日期:2016-05-21

在画UI进程中,有些环境下需求明白statusBar高度:

网上有些方式是多么的:

Rect frame = new Rect();  getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  int statusBarHeight = frame.top;  Log.v("@@@@@@", "the statusbar Height is : " + statusBarHeight);

近似:

int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();  //statusBarHeight是下面所求的情况栏的高度  int titleBarHeight = contentTop - statusBarHeight;  Log.v("@@@@@@", "the titleBar Height is : " + titleBarHeight); 

或:

Rect rect = new Rect();  MainActivity.this.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);  View view = MainActivity.this.getWindow().findViewById(Window.ID_ANDROID_CONTENT);  int topS = rect.top;//情况栏高度  int topT = rect.height() - view.getHeight();  Log.v("@@@@@@", "the statusbar Height is : " + topS);  Log.v("@@@@@@", "the titleBar Height is : " + topT);

在我的手机上,均不可。

后:

public static int getStatusBarHeight(Context context){  Class ? c = null;  Object obj = null;  Field field = null;  int x = 0, 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 = context.getResources().getDimensionPixelSize(x);  Log.v("@@@@@@", "the status bar height is : " + statusBarHeight);  } catch (Exception e1) {  e1.printStackTrace();  return statusBarHeight;  } 

经测试可行。



拒绝套路 试听有礼

数据已加密保证您的信息安全