Android編程實現獲取標題欄、狀態(tài)欄的高度、屏幕大小及模擬Home鍵的方法
更新時間:2015年10月23日 14:52:33 作者:freesonhp
這篇文章主要介紹了Android編程實現獲取標題欄、狀態(tài)欄的高度、屏幕大小及模擬Home鍵的方法,涉及Android獲取手機常見信息的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Android編程實現獲取標題欄、狀態(tài)欄的高度、屏幕大小及模擬Home鍵的方法。分享給大家供大家參考,具體如下:
1. 獲取標題欄高度:
/** * 獲取標題欄的高度 * * @param activity * @return */ public int getTitleHeight(Activity activity) { Rect rect = new Rect(); Window window = activity.getWindow(); window.getDecorView().getWindowVisibleDisplayFrame(rect); int statusBarHeight = rect.top; int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); int titleBarHeight = contentViewTop - statusBarHeight; return titleBarHeight; }
2. 獲取狀態(tài)欄的高度:
/** * * 獲取狀態(tài)欄高度 * * @param activity * @return */ public int getStateHeight(Activity activity) { Rect rect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); return rect.top; }
3. 屏幕大小:
/** * 獲取屏幕寬高 * * @param activity * @return int[0] 寬,int[1]高 */ public int[] getScreenWidthAndSizeInPx(Activity activity) { DisplayMetrics displayMetrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int[] size = new int[2]; size[0] = displayMetrics.widthPixels; size[1] = displayMetrics.heightPixels; return size; }
4. 模擬Home鍵:
/** * 模擬home鍵 * * @param context */ public void goToDestop(Context context) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addCategory(Intent.CATEGORY_HOME); context.startActivity(intent); }
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android用Scroller實現一個可向上滑動的底部導航欄
本篇文章主要介紹了Android用Scroller實現一個可上滑的底部導航欄,具有一定的參考價值,有興趣的小伙伴們可以參考一下2017-07-07Android 中 TabHost與ViewPager結合實現首頁導航效果
這篇文章主要介紹了Android 中 TabHost與ViewPager結合實現首頁導航效果,代碼簡單易懂,感興趣的朋友參考下吧2016-09-09Android開發(fā)之permission動態(tài)權限獲取詳解
這篇文章主要為大家詳細介紹了Android開發(fā)之permission動態(tài)權限獲取,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08Android開發(fā)中使用Intent打開第三方應用及驗證可用性的方法詳解
這篇文章主要介紹了Android開發(fā)中使用Intent打開第三方應用及驗證可用性的方法,結合實例形式分析了Android使用Intent打開第三方應用的三種常用方式及使用注意事項,需要的朋友可以參考下2017-11-11