Android中獲取屏幕相關信息(屏幕大小,狀態欄、標題欄高度)的代碼
阿新 • • 發佈:2019-05-06
status tle efault android中 () 兩個 statusbar create eight 如下代碼內容是關於 Android中獲取屏幕相關信息(屏幕大小,狀態欄、標題欄高度)的代碼。
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); 這段代碼可以插入到Activity的onCreate()函數中。2.獲取標題欄、狀態欄高度: Rect rect = new Rect(); Window win = this.getWindow(); win.getDecorView().getWindowVisibleDisplayFrame(rect); int statusBarHeight = rect.top; int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop(); int titleBarHeight = contentViewTop - Variable.statusBarHeight;
另外,這段代碼不能插入Activity的onCreate()和onResume()函數中,否則得到的兩個值都為0。我的做法是將之插入到onPause()函數中,成功得到兩個正確值。據說可以插入到按鈕等組件的回調函數中,但是本人沒有實驗,就不妄下結論了。
Android中獲取屏幕相關信息(屏幕大小,狀態欄、標題欄高度)的代碼