Android獲取屏幕高度、狀態欄高度、標題欄高度(一)

曬代碼前先了解一下Android屏幕區域的劃分,以下圖(該圖引用自此文http://www.iteye.com/topic/828830輸入圖片說明 輸入圖片說明app

一、 屏幕區域的獲取ide

activity.getWindowManager().getDefaultDisplay();

二、應用區域的獲取函數

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

其中,outRect.top 便是狀態欄高度。 三、view繪製區域獲取ui

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

用繪製區域的outRect.top - 應用區域的outRect.top 便是標題欄的高度。 注意: 若是剛啓動Activity時就要計算這些數據,最好在 onWindowFocusChanged 函數中進行, 不然獲得的某些數據多是錯誤的,好比,應用區域高寬的獲取。 詳細代碼以下:this

public class ScreenSize extends Activity {  
    private TextView mScreenSizeView ;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_screen_size);  
        mScreenSizeView = (TextView) findViewById(R.id.screen_size);  
    }  
  
    @Override  
    public void onWindowFocusChanged(boolean hasFocus) {  
        super.onWindowFocusChanged(hasFocus);  
        if(hasFocus){  
            System.out.println("second");  
            StringBuilder sb = new StringBuilder();  
            Dimension dimen1 = getAreaOne(this);  
            Dimension dimen2 = getAreaTwo(this);  
            Dimension dimen3 = getAreaThree(this);  
            sb.append("Area one : \n\tWidth: "+dimen1.mWidth + ";\tHeight: "+dimen1.mHeight);  
            sb.append("\nArea two: \n\tWidth: "+dimen2.mWidth + ";\tHeight: "+dimen2.mHeight);  
            sb.append("\nArea three: \n\tWidth: "+dimen3.mWidth + ";\tHeight: "+dimen3.mHeight);  
            mScreenSizeView.setText(sb.toString());  
        }  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        getMenuInflater().inflate(R.menu.activity_screen_size, menu);  
        return true;  
    }  
      
    private Dimension getAreaOne(Activity activity){  
        Dimension dimen = new Dimension();  
        Display disp = activity.getWindowManager().getDefaultDisplay();  
        Point outP = new Point();  
        disp.getSize(outP);  
        dimen.mWidth = outP.x ;  
        dimen.mHeight = outP.y;  
        return dimen;  
    }  
    private Dimension getAreaTwo(Activity activity){  
        Dimension dimen = new Dimension();  
      Rect outRect = new Rect();  
      activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);  
      System.out.println("top:"+outRect.top +" ; left: "+outRect.left) ;  
      dimen.mWidth = outRect.width() ;  
      dimen.mHeight = outRect.height();  
        return dimen;  
    }  
    private Dimension getAreaThree(Activity activity){  
        Dimension dimen = new Dimension();  
     // 用戶繪製區域   
        Rect outRect = new Rect();  
        activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);  
        dimen.mWidth = outRect.width() ;  
        dimen.mHeight = outRect.height();  
        // end  
        return dimen;  
    }
private class Dimension {  
    public int mWidth ;  
    public int mHeight ;  
    public Dimension(){}  
}

輸入圖片說明

相關文章
相關標籤/搜索