Android5.0+
状态栏
半透明灰色遮罩
安卓5.0及以上的版本,透明状态栏有一层遮罩一样的,相当于暗色,部分APP完美适配了透明状态栏,状态栏颜色和APP一体了,但是很多APP仍然是状态栏和APP不是同一个颜色,应该如何修改systemui才能将那个暗色的遮罩去掉呢?
去除前效果
直接贴代码,一目了然:
import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.common.helper.R;
import com.readystatesoftware.systembartint.SystemBarTintManager;
/**
* CN: BaseActivity
* Author: JSYL-DINGCL (1144286501@qq.com)
* Date: 2019/10/14
* Des: TODO:
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //系统版本大于19
setTranslucentStatus(true);
}
//状态栏背景着色
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.drawable.shape_statusbar);
//去除灰色遮罩
//Android5.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}else if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT){//Android4.4以上,5.0以下
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
@TargetApi(19)
public 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; // a|=b的意思就是把a和b按位或然后赋值给a 按位或的意思就是先把a和b都换成2进制,然后用或操作,相当于a=a|b
} else {
winParams.flags &= ~bits; //&是位运算里面,与运算 a&=b相当于 a = a&b ~非运算符
}
win.setAttributes(winParams);
}
}
shape_statusbar.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="180"
android:startColor="@color/app_color_theme_1"
android:endColor="@color/app_color_theme_3" />
</shape>
color:
<color name="app_color_theme_alpha_1">#44EF5362</color> <!-- Grapefruit -->
<color name="app_color_theme_alpha_3">#44FFCF47</color> <!-- Sunflower -->