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

ayhandsome 8年前
   <p>注意,数据的获取应该在onWindowFocusChanged函数中进行,防止数据获取错误。</p>    <p>首先声明整个手机屏幕的获取:</p>    <pre>  <code class="language-java">activity.getWindowManager().getDefaultDisplay();</code></pre>    <p>声明整个应用(除了状态栏之外的区域的获取):</p>    <pre>  <code class="language-java">Rect outRect = new Rect();        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);  </code></pre>    <p>得到除了状态栏和标题栏之外的View绘制的区域:</p>    <pre>  <code class="language-java">Rect outRect = new Rect();        activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);  </code></pre>    <p>在得到了绘制的区域之后我们来得到所要计算的高度:</p>    <h2>一、状态栏的高度:</h2>    <pre>  <code class="language-java">Rect frame = new Rect();        getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);        int statusBarHeight = frame.top;  </code></pre>    <p>另外常用的两种方式:</p>    <p>首先是反射的方式:</p>    <pre>  <code class="language-java">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) {                    }</code></pre>    <p>其次:</p>    <pre>  <code class="language-java">int result=0;          int resourceId=getResources().getIdentifier("status_bar_height","dimen","android");          if(resourceId>0){              result=getResources().getDimensionPixelSize(resourceId);          }          return result;</code></pre>    <h2>二、得到标题栏的高度:</h2>    <pre>  <code class="language-java">int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();        //statusBarHeight是上面所求的状态栏的高度        int titleBarHeight = contentTop - statusBarHeight</code></pre>    <h2>三、两种方式得到屏幕的高度:</h2>    <pre>  <code class="language-java">WindowManager windowManager = getWindowManager();        Display display = windowManager.getDefaultDisplay();        screenWidth = display.getWidth();        screenHeight = display.getHeight();  </code></pre>    <pre>  <code class="language-java">DisplayMetrics dm = new DisplayMetrics();     this.getWindowManager().getDefaultDisplay().getMetrics(dm);//this指当前activity    screenWidth =dm.widthPixels;    screenHeight =dm.heightPixels</code></pre>    <p>补充使得屏幕横屏的代码:</p>    <pre>  <code class="language-java">setRequesteOrientation(ActivityInfo.SCREEN_ORIENTATION_LADSCAPE);</code></pre>    <h2>四:获取控件的宽高,一般来说,我们在onCreate里面得到的控件的宽高全是0.采用下面的方法,可以得到真实的宽高</h2>    <pre>  <code class="language-java">//------------------------------------------------方法一            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());                }              });    </code></pre>    <p> </p>    <p>来自:http://blog.csdn.net/james_shu/article/details/53363234</p>    <p> </p>