关键代码:
protected void initSystemBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// Translucent status bar
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// Translucent navigation bar
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
或者在主题中配置
<!-- 透明状态栏主题 style.xml-->
<style name="AppTheme.MyAppTheme">
<!-- Customize your theme here. -->
<item name="android:fitsSystemWindows">false</item>
</style>
<!-- 透明状态栏主题 style.xml(v19)-->
<style name="AppTheme.MyAppTheme">
<!-- Customize your theme here. -->
<item name="android:fitsSystemWindows">false</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
<!-- 透明状态栏主题 style.xml(v21)-->
<style name="AppTheme.MyAppTheme">
<!-- Customize your theme here. -->
<item name="android:fitsSystemWindows">false</item>
<item name="android:statusBarColor">@color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
问题:
1.状态栏透明之后,布局会统一上移,所以需要在第一个布局上面加paddingTop,一般导航栏写成一个控件TopBar,然后每个页面去引用, 可以建values-v19 和values-21文件夹,在dimens.xml里面去配置TopBar的高度和paddingTop
2.透明状态栏会与底部输入框有冲突,导致adjustResize不起作用,输入框不能被软键盘弹起来,解决方法是重写activity的根布局,然后在根布局上设置fitSystemWindows=”true”,这个忘了设置是不行的
/**
* Created by star on 2017/1/20
* 功能: 解决透明状态栏和底部输入框的冲突问题,将activity的根布局替换成该类,需要什么布局就继承哪个ViewGroup,比如需要RelativeLayout就extends RelativeLayout
*/
public class CustomInsetsLinearLayout extends LinearLayout {
private int[] mInsets = new int[4];
public CustomInsetsLinearLayout(Context context) {
super(context);
}
public CustomInsetsLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomInsetsLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public final int[] getInsets() {
return mInsets;
}
@Override
protected final boolean fitSystemWindows(Rect insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Intentionally do not modify the bottom inset. For some reason,
// if the bottom inset is modified, window resizing stops working.
mInsets[0] = insets.left;
mInsets[1] = insets.top;
mInsets[2] = insets.right;
insets.left = 0;
insets.top = 0;
insets.right = 0;
}
return super.fitSystemWindows(insets);
}
@Override
public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
mInsets[0] = insets.getSystemWindowInsetLeft();
mInsets[1] = insets.getSystemWindowInsetTop();
mInsets[2] = insets.getSystemWindowInsetRight();
return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0,
insets.getSystemWindowInsetBottom()));
} else {
return insets;
}
}
}
- fitSystemWindows=”true”意思是布局是否从状态栏下面开始排列
clipToPadding默认为true,如果最上面布局有paddingTop,例如ListView,则表示滑动的时候paddingTop能否被滑走,true为不滑走,false为滑走
状态栏着色
public static void setStatusBarColor(Window window, int color, boolean isAddView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//5.0及以上,不设置透明状态栏,设置会有半透明阴影
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(color);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (isAddView) {
View statusBarView = new View(window.getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getStatusBarHeight(window.getContext()));
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(color);
ViewGroup decorView = (ViewGroup) window.getDecorView();
decorView.addView(statusBarView);
}
ViewGroup contentView = (ViewGroup) window.findViewById(android.R.id.content);
View rootView = contentView.getChildAt(0);
if (rootView instanceof ViewGroup) {
rootView.setFitsSystemWindows(true);
}
}
}