SystemBarTint实现沉浸式状态栏

   看到项目设计图中居然有沉浸式状态栏的效果,(其实以前也看到过的,但是一直没空理他),然后为了实现这个效果,百度了一下,发现这个东西其实并不难。
   现在在as上开发,所以讲下在as如何使用这个第三方库实现效果。
   首先我们在github搜索下systembartint这个库,然后再下载他的jar包到本地,当然也可以使用在线导入。我在这里使用的是在线的,提供大家一个地址吧。在android studio的app.gradle中添加依赖:
   compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
这个库,然后同步下即可。
   好了前面的步骤做好了我们就可以在activity的oncreate()中运用代码了
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_actionbar);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }

        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        //通知栏所需颜色
        tintManager.setStatusBarTintEnabled(true);

        tintManager.setStatusBarTintResource(R.color.statusbar_bg);
        //设置导航栏的颜色
          tintManager.setNavigationBarTintEnabled(true);  
         tintManager.setNavigationBarTintEnabled(true);  

    }

    @TargetApi(19) 
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

好了代码非常简单,我测试了下没有效果,原来还有一个地方没有注意到。那就是在xml文件的最顶级容器上加上一句android:fitsSystemWindows=”true” ,否则是没有效果。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值