状态栏的颜色问题困扰我很久,今天查了些资料,做个总结,对应工程StatusbarDemo
Material Design状态栏
Material Design认为状态栏的颜色应该比actionbar的颜色深一点
5.0以上可以直接用colorPrimaryDark改变状态栏颜色,但是如果要去4.4也一样的话就得借助SystemBarTintManager了。
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
v19 styles.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
v21 styles.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.fish.a2.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
public class MdActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
fitVer19();
}
private void fitVer19() {
//对19-21的做适配
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintResource(R.color.colorPrimaryDark);
tintManager.setStatusBarTintEnabled(true);
}
}
}
效果如下所示
21用的什么方法改变状态栏的颜色?
设置statusBarColor为transparent。以前我以为修改statusBarColor可以改变状态栏颜色,可是结果不行。修改状态栏颜色有2种方法
1、colorPrimaryDark
2、用java代码修改statusBarColor,这里用java代码修改statusBarColor有效果,但是在style里的android:statusBarColor设置值没有任何效果,不知道为什么
getWindow().setStatusBarColor(getResources().getColor(R.color.colorRed));
19用的是android:windowTranslucentStatus让状态栏透明,19用SystemBarTintManager来改变状态栏颜色,实际上是在decorview内加了一个view跟状态栏叠在一起(其实在contentview里加也是可以的)。而21用的是android:statusBarColor让状态栏透明。
如果21也用android:windowTranslucentStatus的话,会导致状态栏颜色很深,不符合MD。
沉浸式效果
要想实现所谓的沉浸式效果,就要把状态栏和toolbar弄成同色。理论上把colorPrimaryDark和colorPrimary的值改为一样就行了,结果4.4没问题,但是5.0,statusbar下边会有一层阴影。
怎么解决,去掉
v21里的
@android:color/transparent
可以看出v19和v21略有不同,v19有个渐变遮罩,而v21没有
全屏
这里的全屏是带有状态栏的全屏,依然用AppTheme.NoActionBar。
CoordinatorLayout内部去掉默认的fitsSystemWindows。
另外对21以上需要加一个属性FLAG_TRANSLUCENT_NAVIGATION
19效果
21效果
其他
https://github.com/laobie/StatusBarUtil提供了一个状态栏颜色相关的库,很不错
参考资料
https://www.zhihu.com/question/31468556
http://blog.csdn.net/lmj623565791/article/details/48649563
http://blog.raffaeu.com/archive/2015/04/11/android-and-the-transparent-status-bar.aspx
http://jaeger.itscoder.com/android/2016/03/27/statusbar-util.html
http://stackoverflow.com/a/30659873/3173263
https://github.com/laobie/StatusBarUtil