|
| 1 | +package com.zoopark.utils; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Context; |
| 5 | +import android.os.Build; |
| 6 | +import android.view.Window; |
| 7 | +import android.view.WindowManager; |
| 8 | + |
| 9 | +/** |
| 10 | + * 状态栏工具类 |
| 11 | + */ |
| 12 | +public class StatusBarUtils { |
| 13 | + |
| 14 | + /** |
| 15 | + * 获得 Status Bar 高度 |
| 16 | + * |
| 17 | + * @param context 上下文 |
| 18 | + * @return int Status Bar 高度 |
| 19 | + */ |
| 20 | + public static int getStatusBarHeight(Context context) { |
| 21 | + int height = 0; |
| 22 | + int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); |
| 23 | + if (resourceId > 0) { |
| 24 | + height = context.getResources().getDimensionPixelSize(resourceId); |
| 25 | + } |
| 26 | + return height; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Hide Status Bar |
| 31 | + * |
| 32 | + * @param activity |
| 33 | + * @param isVisible |
| 34 | + */ |
| 35 | + public static void setStatusBarVisibility(Activity activity, boolean isVisible) { |
| 36 | + if (isVisible) { |
| 37 | + activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 38 | + } else { |
| 39 | + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Return whether the status bar is visible. |
| 45 | + * |
| 46 | + * @param activity |
| 47 | + * @return |
| 48 | + */ |
| 49 | + public static boolean isStatusBarVisible(Activity activity) { |
| 50 | + int flags = activity.getWindow().getAttributes().flags; |
| 51 | + return (flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == 0; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * 设置状态栏透明 |
| 57 | + * |
| 58 | + * @return ture 设置成功 |
| 59 | + */ |
| 60 | + public static boolean setTranslucent(Activity activity) { |
| 61 | + // 在4.4版本以上置StatusBar为透明 |
| 62 | + if (Build.VERSION.SDK_INT > 18) { |
| 63 | + Window window = activity.getWindow(); |
| 64 | + // 设置 StatusBar 为透明显示 |
| 65 | + // 需要在 setContentView 之前完成操作 |
| 66 | + window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, |
| 67 | + WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
| 68 | + return true; |
| 69 | + } else { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 更改状态栏颜色 |
| 76 | + * |
| 77 | + * @param color |
| 78 | + */ |
| 79 | + public static void setStatusBarColor(Activity activity, int color) { |
| 80 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 81 | + Window window = activity.getWindow(); |
| 82 | + window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
| 83 | + window.setStatusBarColor(activity.getResources().getColor(color)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments