本文实例讲述了Android编程实现获取标题栏、状态栏的高度、屏幕大小及模拟Home键的方法。分享给大家供大家参考,具体如下:
1. 获取标题栏高度:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * 获取标题栏的高度 * * @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. 获取状态栏的高度:
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * * 获取状态栏高度 * * @param activity * @return */ public int getStateHeight(Activity activity) { Rect rect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); return rect.top; } |
3. 屏幕大小:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * 获取屏幕宽高 * * @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键:
1
2
3
4
5
6
7
8
9
10
11
|
/** * 模拟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程序设计有所帮助。