状态栏相关
程序员文章站
2022-03-11 21:49:48
package com.benben.commoncore.utils;import android.app.Activity;import android.content.Context;import android.graphics.Color;import android.os.Build;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.....
package com.benben.commoncore.utils; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Build; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import androidx.annotation.ColorRes; import androidx.annotation.NonNull; import com.readystatesoftware.systembartint.SystemBarTintManager; /** * Create by wanghk on 2019-05-28. * Describe:状态栏utils */ public class StatusBarUtils { //修改状态栏文字颜色 public static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) { View decor = activity.getWindow().getDecorView(); if (dark) { decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } else { decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); } } /** * 修改状态栏颜色,支持4.4以上版本 * @param activity * @param colorId */ public static void setStatusBarColor(Activity activity, @ColorRes int colorId) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = activity.getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(activity.getResources().getColor(colorId)); // window.setNavigationBarColor(activity.getResources().getColor(colorId)); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //使用SystemBarTint库使4.4版本状态栏变色,需要先将状态栏设置为透明 // transparencyBar(activity); Window window = activity.getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); SystemBarTintManager tintManager = new SystemBarTintManager(activity); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(colorId); } } /** * 设置状态栏渐变颜色 * * @param activity 目标activity * @param view 目标View */ public static void setGradientColor(@NonNull Activity activity, View view) { ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); View fakeStatusBarView = decorView.findViewById(android.R.id.custom); if (fakeStatusBarView != null) { decorView.removeView(fakeStatusBarView); } setRootView(activity, false); setTransparentForWindow(activity); setPaddingTop(activity, view); } /** * 设置根布局参数 * * @param activity 目标activity * @param fitSystemWindows 是否预留toolbar的高度 */ private static void setRootView(Activity activity, boolean fitSystemWindows) { ViewGroup parent = activity.findViewById(android.R.id.content); for (int i = 0, count = parent.getChildCount(); i < count; i++) { View childView = parent.getChildAt(i); if (childView instanceof ViewGroup) { childView.setFitsSystemWindows(fitSystemWindows); ((ViewGroup) childView).setClipToPadding(fitSystemWindows); } } } /** * 设置透明状态栏 * * @param activity 目标界面 */ public static void setTransparentForWindow(@NonNull Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().setStatusBarColor(Color.TRANSPARENT); activity.getWindow() .getDecorView() .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow() .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } /** * 增加View的paddingTop,增加的值为状态栏高度 (智能判断,并设置高度) * * @param context 目标Context * @param view 需要增高的View */ public static void setPaddingTop(Context context, @NonNull View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { ViewGroup.LayoutParams lp = view.getLayoutParams(); if (lp != null && lp.height > 0 && view.getPaddingTop() == 0) { lp.height += getStatusBarHeight(context); view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context), view.getPaddingRight(), view.getPaddingBottom()); // if(view instanceof ViewGroup){ // for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { // View childView = ((ViewGroup) view).getChildAt(i); // if (childView instanceof TextView) { // childView.setPadding(0, getStatusBarHeight(context)-DensityUtil.dip2px(context,2), // 0, 0); // } // } // } } } } /** * 获取状态栏高度 * * @param context 目标Context */ public static int getStatusBarHeight(Context context) { // 获得状态栏高度 int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); } }
本文地址:https://blog.csdn.net/qq7915/article/details/108807640