DrawerLayout 和 沉浸式布局

DrawerLayout  这个和普通的ViewGroup设置沉浸式 不一样。

   普通的只要设置背景,给一个图片或者颜色,状态栏就会变成相应的背景色。(这是在工程已经设置好了在沉浸式下)。

   当你发现用DrawerLayout 作为父容器时,给DrawerLayout 设置背景,状态栏还是系统默认色,起初我修改了主题中的选项

 在 styles.xml 里面。

   对应的主题下添加

<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
 
colorPrimaryDark  为你定义在colors.xml的颜色。
 

  这样可以通过修改主题颜色达到效果,但是其他控件如果用到这个颜色,也会被修改掉,所以这个不是最佳的。

<!--  察看源码发现,DrawerLayout 的onDraw里面有画状态栏  -->

@Override
public void onDraw(Canvas c) {
    super.onDraw(c);
    if (mDrawStatusBarBackground && mStatusBarBackground != null) {
        final int inset = IMPL.getTopInset(mLastInsets);
        if (inset > 0) {
            mStatusBarBackground.setBounds(0, 0, getWidth(), inset);
            mStatusBarBackground.draw(c);
        }
    }
}

mStatusBarBackground ,可以通过设置
public void setStatusBarBackgroundColor(@ColorInt int color) {
    mStatusBarBackground = new ColorDrawable(color);
    invalidate();
}
或者
public void setStatusBarBackground(int resId) {
    mStatusBarBackground = resId != 0 ? ContextCompat.getDrawable(getContext(), resId) : null;
    invalidate();
}
也可以
public void setStatusBarBackground(Drawable bg) {
    mStatusBarBackground = bg;
    invalidate();
}



这样就不会影响到其他了。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值