Android 獲得狀態欄、標題欄以及控件的高度

ayhandsome 7年前發布 | 8K 次閱讀 安卓開發 Android開發 移動開發

注意,數據的獲取應該在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());  
            }    
        });    

 

來自:http://blog.csdn.net/james_shu/article/details/53363234

 

 本文由用戶 ayhandsome 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!