StatusBarUtils工具类

public class StatusBarUtils {public static void initStatusBar(Window window, View view, int color, boolean transparent) {// 如果系统版本大于等于 Android 5.0(Lollipop)if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {if (transparent) {// 设置状态栏为透明,并且布局充满全屏window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);window.setStatusBarColor(Color.TRANSPARENT);} else {// 设置状态栏颜色为传入的颜色值window.setStatusBarColor(color);}// 如果系统版本大于等于 Android 6.0(Marshmallow)if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 设置状态栏文字颜色为传入的颜色值window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);}}// 设置顶部内边距等于状态栏高度(使内容在状态栏的下方显示)int statusBarHeight = ViewUtils.getStatusBarHeight(view.getContext());view.setPadding(0, statusBarHeight, 0, 0);}/** * 隐藏状态栏 */public static void hideStatusBar(Activity activity) {// 获取窗口Window window = activity.getWindow();// 对Android版本进行检查,选择不同的方法实现隐藏状态栏if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// API Level 19 及以上(Android 4.4及以上)View decorView = window.getDecorView();int option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_FULLSCREEN| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;decorView.setSystemUiVisibility(option);} else if (Build.VERSION.SDK_INT >= 16) {// API Level 16 到 18 (Android 4.1 到 Android 4.3)View decorView = window.getDecorView();int option = View.SYSTEM_UI_FLAG_FULLSCREEN;decorView.setSystemUiVisibility(option);} else {// API Level 16以下(Android 4.1以下)window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);}}/** * 显示状态栏 * * @param activity 当前的Activity */public static void showStatusBar(Activity activity) {// 获取窗口Window window = activity.getWindow();// 对Android版本进行检查,选择不同的方法实现显示状态栏if (Build.VERSION.SDK_INT < 16) {// API Level 16以下(Android 4.1以下)window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);} else {// API Level 16 及以上(Android 4.1及以上)View decorView = window.getDecorView();int option = View.SYSTEM_UI_FLAG_VISIBLE;decorView.setSystemUiVisibility(option);}}}

ViewUtils工具类

public class ViewUtils {/** * 获取状态栏高度 * * @param context 应用上下文 * @return int 状态栏高度 */public static int getStatusBarHeight(Context context) {int result = 0;int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");if (resourceId > 0) {result = context.getResources().getDimensionPixelSize(resourceId);}return result;}/** * 计算Item之间的间距 * * @param columnCount 列数 * @param ratio 宽高比 * @return int[] 包含水平和垂直间距的数组 */public static int[] calculateSpacing(int columnCount, float ratio) {// 计算屏幕宽度DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();int screenWidth = displayMetrics.widthPixels;// 计算每个item的宽度int itemWidth = (screenWidth - (columnCount + 1) * 10) / columnCount;// 计算item高度int itemHeight = (int) (itemWidth * ratio);// 计算水平间距和垂直间距int horizontalSpacing = (screenWidth - itemWidth * columnCount) / (columnCount + 1);int verticalSpacing = (int) (horizontalSpacing * ratio);return new int[]{horizontalSpacing, verticalSpacing};}}

用法

以下是一个简单的示例,演示如何在 Activity 中调用 StatusBarUtils 类和 ViewUtils 类中的方法来初始化状态栏和计算间距:

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 初始化状态栏Window window = getWindow();View rootView = getWindow().getDecorView().findViewById(android.R.id.content);int statusBarColor = Color.BLUE;boolean isTransparent = false;StatusBarUtils.initStatusBar(window, rootView, statusBarColor, isTransparent);// 隐藏状态栏StatusBarUtils.hideStatusBar(this);// 显示状态栏(可选)// StatusBarUtils.showStatusBar(this);// 计算间距int columnCount = 2;float ratio = 1.5f;int[] spacing = ViewUtils.calculateSpacing(columnCount, ratio);int horizontalSpacing = spacing[0];int verticalSpacing = spacing[1];// 设置水平和垂直间距RecyclerView recyclerView = findViewById(R.id.recyclerView); // 假设使用 RecyclerViewrecyclerView.addItemDecoration(new GridSpacingItemDecoration(columnCount, horizontalSpacing, verticalSpacing, true));}}

请确保将上述代码中的 R.layout.activity_main 替换为你的实际布局文件,并根据需要调整状态栏颜色、列数、宽高比等参数。这样,在你的 Activity 中就可以使用状态栏工具类和视图工具类中的方法来实现相应的功能