查阅各大网站,最后结合自己的时间,总结出了两种可行的方法
一.修改样式文件:
1.增加values-v19文件夹,再其中的styles.xml中加上:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- toolbar(actionbar)颜色 -->
<item name="colorPrimary">@color/colorPrimary</item>
<!-- 状态栏颜色 -->
<item name="colorPrimaryDark">@color/colorPrimary</item>
<!-- toolbar标题颜色 -->
<item name="android:textColorPrimary">@color/white</item>
<!-- 窗口的背景颜色 -->
<item name="android:windowBackground">@color/colorPrimary</item>
<!-- translucent system bars-->
<item name="android:windowTranslucentStatus" >true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
二.使用开源库
1.android studio中加入依赖:
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
2.在activity的onCreate()中调用下面方法:
/**
* Apply KitKat specific translucency.
*/
private void applyKitKatTranslucency() {
// KitKat translucent navigation/status bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
SystemBarTintManager mTintManager = new SystemBarTintManager(this);
mTintManager.setStatusBarTintEnabled(true);
mTintManager.setNavigationBarTintEnabled(true);
// mTintManager.setTintColor(0xF00099CC);
mTintManager.setTintDrawable(UIElementsHelper
.getGeneralActionBarBackground(this));
}
}
@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"
否则标题栏是白色的。