Android透明状态栏(4.4以上版本)

关键代码:

protected void initSystemBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // Translucent navigation bar
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

问题:
1.状态栏透明之后,布局会统一上移,所以需要在第一个布局上面加paddingTop,一般导航栏写成一个控件TopBar,然后每个页面去引用, 可以建values-v19 和values-21文件夹,在dimens.xml里面去配置TopBar的高度和paddingTop
2.透明状态栏会与底部输入框有冲突,导致adjustResize不起作用,输入框不能被软键盘弹起来,解决方法是重写activity的根布局,然后在根布局上设置fitSystemWindows=”true”,这个忘了设置是不行的

/**
 * Created by star on 2017/1/20
 * 功能: 解决透明状态栏和底部输入框的冲突问题,将activity的根布局替换成该类,需要什么布局就继承哪个ViewGroup,比如需要RelativeLayout就extends RelativeLayout
 */
public class CustomInsetsLinearLayout extends LinearLayout {
    private int[] mInsets = new int[4];

    public CustomInsetsLinearLayout(Context context) {
        super(context);
    }

    public CustomInsetsLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomInsetsLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public final int[] getInsets() {
        return mInsets;
    }

    @Override
    protected final boolean fitSystemWindows(Rect insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // Intentionally do not modify the bottom inset. For some reason,
            // if the bottom inset is modified, window resizing stops working.

            mInsets[0] = insets.left;
            mInsets[1] = insets.top;
            mInsets[2] = insets.right;

            insets.left = 0;
            insets.top = 0;
            insets.right = 0;
        }

        return super.fitSystemWindows(insets);
    }

    @Override
    public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            mInsets[0] = insets.getSystemWindowInsetLeft();
            mInsets[1] = insets.getSystemWindowInsetTop();
            mInsets[2] = insets.getSystemWindowInsetRight();
            return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0,
                    insets.getSystemWindowInsetBottom()));
        } else {
            return insets;
        }
    }
}
  1. fitSystemWindows=”true”意思是布局是否从状态栏下面开始排列
  2. clipToPadding默认为true,如果最上面布局有paddingTop,例如ListView,则表示滑动的时候paddingTop能否被滑走,true为不滑走,false为滑走
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33666539/article/details/54669857
文章标签: Android透明状
个人分类: Android
想对作者说点什么? 我来说一句

Android状态栏透明(沉浸式)Demo

2015年08月28日 232KB 下载

没有更多推荐了,返回首页

不良信息举报

Android透明状态栏(4.4以上版本)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭