1、项目中需要在开机图时去掉状态栏
(1)实现方法如下:
在setContentView之后设置
//去掉状态栏
if (Build.VERSION.SDK_INT >= 21) {
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
getWindow().setNavigationBarColor(Color.TRANSPARENT);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
(2)实现效果如下:
2、需要改变状态栏颜色为渐变色标
(1)实现方法如下:
在setContentView之后设置
//设置状态栏颜;
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
initStatusBar();
getWindow().getDecorView().removeOnLayoutChangeListener(this);
}
});
private void initStatusBar() {
if (statusBarView == null) {
//利用反射机制修改状态栏背景
int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
statusBarView = getWindow().findViewById(identifier);
}
if (statusBarView != null) {
statusBarView.setBackgroundResource(R.drawable.shape_title_bg);
}
}
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--angle 渐变角度,0:左到右;90:下到上;180:右到左;270:上到下-->
<gradient
android:startColor="@color/titleStartColor"
android:endColor="@color/titleEndColor"
android:angle="180"></gradient>
</shape>
(2)实现效果如下: