1、Activity界面的劃分html
簡單說明一下(上圖Activity採用默認Style,狀態欄和標題欄都會顯示):最大的草綠色區域是屏幕界面,紅色次大區域咱們稱之爲「應用程序界面區域」,最小紫色的區域咱們稱之爲「View繪製區域」;屏幕頂端、應用界面區以外的那部分顯示手機電池網絡運營商信息的爲「狀態欄」,應用區域頂端、View繪製區外部顯示Activity名稱的部分咱們稱爲「標題欄」。android
/** * 獲取狀態欄高度——方法1 * */ int statusBarHeight1 = -1; //獲取status_bar_height資源的ID int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { //根據資源ID獲取響應的尺寸值 statusBarHeight1 = getResources().getDimensionPixelSize(resourceId); } Log.e("-------", "狀態欄-方法1:" + statusBarHeight1)
/** * 獲取狀態欄高度——方法2 * */ int statusBarHeight2 = -1; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusBarHeight2 = getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } Log.e("-------", "狀態欄-方法2:" + statusBarHeight2);
/** * 獲取狀態欄高度——方法3 * 應用區的頂端位置即狀態欄的高度 * *注意*該方法不能在初始化的時候用 * */ Rect rectangle= new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle); //高度爲rectangle.top-0仍爲rectangle.top Log.e("-------", "狀態欄-方法3:" + rectangle.top);
/** * 獲取狀態欄高度——方法4 * 狀態欄高度 = 屏幕高度 - 應用區高度 * *注意*該方法不能在初始化的時候用 * */ //屏幕 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); //應用區域 Rect outRect1 = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1); int statusBar = dm.heightPixels - outRect1.height(); //狀態欄高度=屏幕高度-應用區域高度 Log.e("------------", "狀態欄-方法4:" + statusBar);
三、4這兩種方法其實本質是同樣,因此若是單單獲取statusBar高度而不獲取titleBar高度時也不推薦你們使用,理由同上方法3網絡
//屏幕 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); Log.e("-------", "屏幕高:" + dm.heightPixels); //應用區域 Rect outRect1 = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1); Log.e("------", "應用區頂部" + outRect1.top); Log.e("-------", "應用區高" + outRect1.height()); //View繪製區域 Rect outRect2 = new Rect(); getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect2); Log.e("--------", "View繪製區域頂部-錯誤方法:" + outRect2.top); //不能像上邊同樣由outRect2.top獲取,這種方式得到的top是0,多是bug吧 int viewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop(); //要用這種方法 Log.e("--------", "View繪製區域頂部-正確方法:" + viewTop); Log.e("--------", "View繪製區域高度:" + outRect2.height());
有時候獲取View繪製區時列出來的那個outRect2.top=0app
/** * 獲取標題欄高度-方法1 * 標題欄高度 = View繪製區頂端位置 - 應用區頂端位置(也能夠是狀態欄高度,獲取狀態欄高度方法3中說過了) * */ int titleHeight1 = viewTop - outRect1.top; Log.e("--------", "標題欄高度-方法1:" + titleHeight1);
/** * 獲取標題欄高度-方法2 * 標題欄高度 = 應用區高度 - View繪製區高度 * */ int titleHeight2 = outRect1.height() - outRect2.height(); Log.e("----------", "標題欄高度-方法2:" + titleHeight2);
*注意* spa
(1)無論你是否設置全屏模式,或是不顯示標題欄,在使用獲取狀態欄高度方法1和獲取狀態欄高度方法2都會測量到狀態欄的高度,理解原理就不難解釋——系統資源屬性是固定的、真實的,無論你是否隱瞞(隱藏或者顯示),它都在那裏;code
(2)可是若使用獲取狀態欄高度方法3和獲取狀態欄高度方法4,以及獲取標題欄高度方法1和獲取標題欄高度方法2,都是依賴於WMS,是在界面構建後根據View獲取的,因此顯示了就有高度,不顯示天然沒高度了。htm
下面是驗證blog
先設置Activity爲全屏遊戲
<activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
屏幕各區域獲取不變;圖片
輸出StatusBar和titleBar高度信息
int titleHeight1 = viewTop - outRect1.top; Log.e("--------", "驗證Statue高度:" + titleHeight1); Log.e("--------", "驗證Title高度:" + outRect1.top);