沉浸式透明状态栏
关键代码:
/**
* 透明状态栏
*
* @param window
* @param statusBarBlackText 状态栏上面的文字是否显示黑色
*/
public static void translucentStatusBar(Window window, boolean statusBarBlackText) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int visibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
if (statusBarBlackText && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
visibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
window.getDecorView().setSystemUiVisibility(visibility);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Translucent status bar
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
或者在style中配置
<!-- 透明状态栏主题 style.xml-->
<style name="TranslucentStatusTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
<!-- 透明状态栏主题 style.xml(v19)-->
<style name="TranslucentStatusTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowTranslucentStatus">true</item>
</style>
<!-- 透明状态栏主题 style.xml(v21)-->
<style name="TranslucentStatusTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:statusBarColor">@color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
问题:
- 上面使用xml做透明状态栏,有一个缺点就是5.0以上状态栏会有透明阴影。
- 状态栏透明之后,布局会统一上移,所以需要在第一个布局上面加paddingTop,一般导航栏写成一个控件TopBar,然后每个页面去引用, 可以建values-v19 ,在dimens.xml里面去配置TopBar的高度和paddingTop(注意4.4做不到透明状态栏,所以它内部的值跟其他不一样),或者调用如下代码:
/**
* 在透明状态栏的情况下,给最上面的view重新设置高度和paddingTop
*
* @param view 要设置的view
*/
public static void setTranslucentHeightAndPaddingTop(final View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
view.post(new Runnable() {
@Override
public void run() {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = view.getMeasuredHeight() + getStatusBarHeight(view.getContext());
view.setPadding(view.getPaddingLeft(),
view.getPaddingTop() + getStatusBarHeight(view.getContext()),
view.getPaddingRight(),
view.getPaddingBottom());
view.setLayoutParams(layoutParams);
}
});
}
}
- 透明状态栏会与底部输入框有冲突,导致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为滑走
状态栏着色
这种方式跟上面沉浸式状态栏是不同的原理,上面是让布局从状态栏处(即屏幕最左上角)开始排列,布局完全自己定制,实际上如果布局最上面是张图片,想让图片透过状态栏,适合用上面的方式,但是如果只是想做到让状态栏颜色和topBar的颜色保持一致或者改变状态栏颜色,这种方式比较好一些。
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) {
//4.4没有setStatusBarColor,所以通过addView来设置状态栏颜色
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);
}
}
}
public static int getStatusBarHeight(Context context) {
Resources resources = context.getResources();
int result = 0;
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId);
}
return result;
}
颜色可以从主题中获取
//调用
getThemeColorByAttrId(getTheme(), R.attr.colorPrimaryDark);
//获取
public static int getThemeColorByAttrId(Resources.Theme theme, int attr) {
TypedValue typedValue = new TypedValue();
theme.resolveAttribute(attr, typedValue, true);
return typedValue.data;
}
颜色在主题中配置
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- customize the color palette -->
//状态栏颜色
<item name="colorPrimaryDark">@color/blue</item>
//actionBar等的颜色
<item name="colorPrimary">@color/blue</item>
//某些原生控件默认字颜色
<item name="colorAccent">@color/red</item>
</style>
如果不同的界面状态栏颜色不同,只需要继承原有主题,重写相应颜色就行
<style name="RedStatusBarTheme" parent="MyTheme">
<item name="colorPrimaryDark">@color/red</item>
</style>
3 假如状态栏颜色是白色,那么需要区分sdk版本,6.0以上才能做到让状态栏上面的字变成黑色(minSdk = 19)
//styles.xml 23以下做不到状态栏上面的字是黑色,为了显示出上面的字,不能用白色,可以用灰色代替
<style name="WhiteStatusBarTheme" parent="MyTheme">
<item name="colorPrimaryDark">@color/status_bar_gray</item>
</style>
//v23/styles.xml
<style name="WhiteStatusBarTheme" parent="MyTheme">
<item name="colorPrimaryDark">@color/white</item>
<item name="android:windowLightStatusBar">true</item><!--控制状态栏文字和图标的颜色,避免与状态栏背景冲突。低版本中状态栏不要设置为白色 v23+ -->
</style>