SystemBarTint实现自定义StatusBar颜色

本文介绍了如何使用SystemBarTint库来改变Android应用的StatusBar颜色,通过添加特定代码和设置,实现界面的美化,解决了头部内容被遮挡的问题。
摘要由CSDN通过智能技术生成

自定义Statusbar颜色十分漂亮,比原本黑色的高端太多


在V7包中使用AppbarLayout和Toolbar可以实现漂亮的效果,用Android Studio新建一个工程就可以看到。(前提别新建一个空工程)


今天使用SystemBarTint来实现。


首先下载相应的jar包,下载地址


然后导入工程,具体导入就不多说了。

然后导入后看代码:


直接在SetContentView下添加一下内容

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);//通知栏所需颜色,自定义即可
然后是setTranslucentStatus()方法。

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

官方demo:

public class MatchActionBarActivity extends Activity { 

	@Override
	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);//通知栏所需颜色

	}

	@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"即可。

好了,一个炫酷的StatusBar就出现了。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值