设置状态栏颜色
if (Build.VERSION.SDK_INT>21){getWindow().setStatusBarColor(getResources().getColor(R.color.mainc));
}
方法2
<color name="colorPrimary">#3F51B5</color>
//取消标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 隐藏状态栏——全屏模式
1、方法1
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
2、方法2
if (Build.VERSION.SDK_INT >= 19) {View decorView = getWindow().getDecorView();decorView.setSystemUiVisibility(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);
}
设置状态栏背景色,沉浸模式
if (Build.VERSION.SDK_INT >= 21) {View decorView = getWindow().getDecorView();int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;decorView.setSystemUiVisibility(option);getWindow().setNavigationBarColor(Color.TRANSPARENT);getWindow().setStatusBarColor(Color.TRANSPARENT);
}
设置状态栏字体色
getWindow().setNavigationBarColor(Color.TRANSPARENT);//底部软键盘背景色getWindow().setStatusBarColor(Color.TRANSPARENT);//状态栏背景色
getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_VISIBLE);//白色
// getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//黑色
透明状态工具类:
/*** @author : LGQ* @date : 2020/07/01 20* @desc :*/public class StatusDelegate {private final int mStatusHeight;private boolean mFitStatusBar;public StatusDelegate(Context context) {this(context, false);}public StatusDelegate(Context context, boolean fitStatusBar) {mStatusHeight = getStatusHeight(context);mFitStatusBar = fitStatusBar;}public int getStatusHeight() {return mStatusHeight;}/*** 是否适配状态栏** @return*/public boolean isFitStatusBar() {return mFitStatusBar;}/*** 设置适配状态栏** @param fitStatusBar*/public void setFitStatusBar(boolean fitStatusBar) {mFitStatusBar = fitStatusBar;}/*** 可以设置透明状态栏** @return*/public boolean canTranslucentStatus() {return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;}/*** 是否应该去适配状态栏** @return*/public boolean toFitStatusBar() {return isFitStatusBar() & canTranslucentStatus();}private int getStatusHeight(Context context) {int result = 0;try {Resources resources = context.getResources();int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");if (resourceId > 0) {int sizeOne = context.getResources().getDimensionPixelSize(resourceId);int sizeTwo = Resources.getSystem().getDimensionPixelSize(resourceId);if (sizeTwo >= sizeOne) {return sizeTwo;} else {float densityOne = context.getResources().getDisplayMetrics().density;float densityTwo = Resources.getSystem().getDisplayMetrics().density;float f = sizeOne * densityTwo / densityOne;return (int) ((f >= 0) ? (f + 0.5f) : (f - 0.5f));}}} catch (Resources.NotFoundException ignored) {return 0;}return result;} }
public class StatusBarView extends View {private static int mStatusBarHeight;private StatusDelegate mStatusDelegate;public StatusBarView(Context context) {this(context, null);}public StatusBarView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);mStatusDelegate = new StatusDelegate(context);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int newHeightSpec;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {newHeightSpec = MeasureSpec.makeMeasureSpec(mStatusDelegate.getStatusHeight(), MeasureSpec.EXACTLY);} else {newHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY);}super.onMeasure(widthMeasureSpec, newHeightSpec);} }
引用
<cn.dlc.dalianmaomeikuang.utils.StatusBarViewandroid:id="@+id/topli"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/transparent"></cn.dlc.dalianmaomeikuang.utils.StatusBarView>