#3F51B5//取消标..._android 状态栏工具类">
赞
踩
- 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); } @Override protected 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.StatusBarView android:id="@+id/topli" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/transparent"></cn.dlc.dalianmaomeikuang.utils.StatusBarView>
ViewCompat.setOnApplyWindowInsetsListener(allli, new OnApplyWindowInsetsListener() { public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { Insets systemInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding( 0, 0, // max(windowInsets.displayCutout?.safeInsetTop ?: 0, systemInsets.top), 0, systemInsets.bottom); return insets.consumeSystemWindowInsets(); } }); WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
private fun openFitsWindow(){ if(isNeedPadding){ ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets -> //防止UI冲突 val systemInsets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) view.updatePadding( binding.root.paddingLeft, systemInsets.top, // max(windowInsets.displayCutout?.safeInsetTop ?: 0, systemInsets.top), binding.root.paddingRight, systemInsets.bottom ) WindowInsetsCompat.CONSUMED } WindowCompat.setDecorFitsSystemWindows(window, false) } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。