个人总结:使用第三方实现沉浸式状态栏

网上很多各种教程,沉浸式,透明状态栏等等,有些都是很久很久以前的,弄起来也费时费力,就是最终可能都还没实现。

所以这里个人总结个最新的,使用第三方库实现的,地址:https://github.com/jgilfelt/SystemBarTint

直接上代码,个人是直接创建一个ActivityBase,然后其他的activity继承它就可以了。

ActivityBase.java

public class ActivityBase extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//只对api19以上版本有效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        //为状态栏着色
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);

//状态栏颜色
        tintManager.setStatusBarTintResource(R.color.title_color);
}

@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);
}
}

创建基类,其他的继承就可以了,其他不需要加其他的东西,有些说了很多要加android:fitsSystemWindows="true" 还有 android:clipToPadding="true"这两个属性,相信新手一看就晕了,我刚开始看的时候也晕,可以直接就用我上面的activity就可以实现了,但还有一个问题,假如你的activtiy里面含有editText,可能activity的xml文件中就需要加上

android:fitsSystemWindows="true"属性了。在这留下一个问题,就是说想给fragmentActivity的状态栏换不同颜色,因为每个fragment的头布局颜色不一样,所以想切换不同的fragment然后状态栏颜色不一样,请懂的朋友解答。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ling9400

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值