#3F51B5//取消标..._android 状态栏工具类">
当前位置:   article > 正文

Android 隐藏状态栏,沉浸式状态栏,状态栏背景色,状态栏字体色,透明状态工具类_android 状态栏工具类

android 状态栏工具类

   

设置状态栏颜色

  1. if (Build.VERSION.SDK_INT>21){
  2. getWindow().setStatusBarColor(getResources().getColor(R.color.mainc));
  3. }

方法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

  1. if (Build.VERSION.SDK_INT >= 19) {
  2. View decorView = getWindow().getDecorView();
  3. decorView.setSystemUiVisibility(
  4. View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  5. | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  6. | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  7. | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  8. | View.SYSTEM_UI_FLAG_FULLSCREEN
  9. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
  10. }

设置状态栏背景色,沉浸模式

  1. if (Build.VERSION.SDK_INT >= 21) {
  2. View decorView = getWindow().getDecorView();
  3. int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  4. | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  5. | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
  6. decorView.setSystemUiVisibility(option);
  7. getWindow().setNavigationBarColor(Color.TRANSPARENT);
  8. getWindow().setStatusBarColor(Color.TRANSPARENT);
  9. }


设置状态栏字体色

  1. getWindow().setNavigationBarColor(Color.TRANSPARENT);//底部软键盘背景色
  2. 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)
        }

    }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/791078
推荐阅读
相关标签
  

闽ICP备14008679号