/**
* 设置透明状态栏
* <p>
* 可在Activity的onCreat()中调用
* <p>
* 注意:需在顶部控件布局中加入以下属性让内容出现在状态栏之下:
* android:clipToPadding="true" // true 会贴近上层布局 ; false 与上层布局有一定间隙
* android:fitsSystemWindows="true" //true 会保留actionBar,title,虚拟键的空间 ; false 不保留
*
* @param activity activity
*/
public static void setTransparentStatusBar(Activity activity) {
//5.0及以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decorView = activity.getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
//4.4到5.0
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
}
}
然后在布局配置颜色,最上面那个控件的颜色就是statusbar的颜色