Android 获得状态栏、标题栏以及控件的高度

2,403 阅读2分钟
原文链接: blog.csdn.net

注意,数据的获取应该在onWindowFocusChanged函数中进行,防止数据获取错误。 首先声明整个手机屏幕的获取:

activity.getWindowManager().getDefaultDisplay();

声明整个应用(除了状态栏之外的区域的获取):

    Rect outRect = new Rect();  
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);  

得到除了状态栏和标题栏之外的View绘制的区域:

    Rect outRect = new Rect();  
    activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);  

在得到了绘制的区域之后我们来得到所要计算的高度: 一、状态栏的高度:

    Rect frame = new Rect();  
    getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
    int statusBarHeight = frame.top;  

另外常用的两种方式: 首先是反射的方式:

    int statusBarHeight=0;
  try {
  Class clazz=Class.forName("com.android.internal.R$dimen");
  Object object=clazz.newInstance();
  Field field=clazz.getField("status_bar_height");
  //反射出该对象中status_bar_height字段所对应的在R文件的id值
  //该id值由系统工具自动生成,文档描述如下:
  //The desired resource identifier, as generated by the aapt tool.
  int id = Integer.parseInt(field.get(object).toString());
  System.out.println("id="+id);
  //依据id值获取到状态栏的高度,单位为像素
  statusBarHeight = context.getResources().getDimensionPixelSize(id);
  System.out.println("statusBarHeight="+statusBarHeight+"pixel");
  } catch (Exception e) {
  
  }

其次:

 int result=0;
        int resourceId=getResources().getIdentifier("status_bar_height","dimen","android");
        if(resourceId>0){
            result=getResources().getDimensionPixelSize(resourceId);
        }
        return result;

二、得到标题栏的高度:

    int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();  
    //statusBarHeight是上面所求的状态栏的高度  
    int titleBarHeight = contentTop - statusBarHeight  

三、两种方式得到屏幕的高度:

    WindowManager windowManager = getWindowManager();  
    Display display = windowManager.getDefaultDisplay();  
    screenWidth = display.getWidth();  
    screenHeight = display.getHeight();  
DisplayMetrics dm = new DisplayMetrics();   
this.getWindowManager().getDefaultDisplay().getMetrics(dm);//this指当前activity  
screenWidth =dm.widthPixels;  
screenHeight =dm.heightPixels

补充使得屏幕横屏的代码:

setRequesteOrientation(ActivityInfo.SCREEN_ORIENTATION_LADSCAPE);

四:获取控件的宽高,一般来说,我们在onCreate里面得到的控件的宽高全是0.采用下面的方法,可以得到真实的宽高

 //------------------------------------------------方法一  
        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
        imageView.measure(w, h);  
        int height =imageView.getMeasuredHeight();  
        int width =imageView.getMeasuredWidth();  
        textView.append("\n"+height+","+width);  

    此方法会加载onMeasure三次  



        //-----------------------------------------------方法二  
        ViewTreeObserver vto = imageView.getViewTreeObserver();  
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {  
            public boolean onPreDraw() {  
                int height = imageView.getMeasuredHeight();  
                int width = imageView.getMeasuredWidth();  
                textView.append("\n"+height+","+width);  
                return true;  
            }  
        });  

    此方法会加载onMeasure二次,但是回调函数会回调很多次  

     //-----------------------------------------------方法三     
        ViewTreeObserver vto2 = imageView.getViewTreeObserver();    
        vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
            @Override    
            public void onGlobalLayout() {  
                imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);    
                textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth());  
            }    
        });