啥也不说了,先上效果图:
设置状态栏透明:
protected void setSystemBarTransparent(){
getWindow().setStatusBarColor(Color.TRANSPARENT);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
给状态栏设置颜色:
1.首先调用上面的setTranslucentStatus()方法设置状态栏透明,才可以设置自己的颜色。
2.获取状态栏高度,创建view,设置需要的背景色,高度设置为状态栏高度,该view就作为状态栏的底色。然后添加view到Activity的decorView。
private void setSystemColor(){
ViewGroup decorViewGroup = (ViewGroup) getWindow().getDecorView();
View statusBarView = new View(getApplicationContext());
int statusBarHeight = getStatusBarHeight(getApplicationContext());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, statusBarHeight);
params.gravity = Gravity.TOP;
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(Color.parseColor("#2ecc71"));
decorViewGroup.addView(statusBarView);
}
private int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
Resources res = context.getResources();
int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = res.getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
3.此时ActionBar显示的是被顶到头部了,显示不全,这时就需要在该Activity的跟布局上加上 android:fitsSystemWindows="true"。它是用来告诉view, 为系统UI提供空间的, 免得让系统UI覆盖在我们的布局上了。fitSystemWindows方法代码:
protected boolean fitSystemWindows(Rect insets) {
if ((mPrivateFlags3 & PFLAG3_APPLYING_INSETS) == 0) {
if (insets == null) {
// Null insets by definition have already been consumed.
// This call cannot apply insets since there are none to apply,
// so return false.
return false;
}
try {
mPrivateFlags3 |= PFLAG3_FITTING_SYSTEM_WINDOWS;
return dispatchApplyWindowInsets(new WindowInsets(insets)).isConsumed();
} finally {
mPrivateFlags3 &= ~PFLAG3_FITTING_SYSTEM_WINDOWS;
}
} else {
return fitSystemWindowsInt(insets);
}
}
Activity全屏设置
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
去掉华为、一加等手机的虚拟菜单栏
public void hideNavigationBar() {
int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
if (Build.VERSION.SDK_INT >= 19) {
uiFlags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; //SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hide navigation bars - compatibility: building API level is lower thatn 19, use magic number directly for higher API target level
} else {
uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
}
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(uiFlags);
}
View.SYSTEM_UI_FLAG_VISIBLE:显示状态栏,Activity不全屏显示(恢复到有状态的正常情况)。
View.INVISIBLE:隐藏状态栏,同时Activity会伸展全屏显示。
View.SYSTEM_UI_FLAG_FULLSCREEN:Activity全屏显示,且状态栏被隐藏覆盖掉。
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN:Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮住。
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION:效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
View.SYSTEM_UI_LAYOUT_FLAGS:效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION:隐藏虚拟按键(导航栏)。有些手机会用虚拟按键来代替物理按键。
View.SYSTEM_UI_FLAG_LOW_PROFILE:状态栏显示处于低能显示状态(low profile模式),状态栏上一些图标显示会被隐藏。
这里我需要传入的是View.SYSTEM_UI_FLAG_FULLSCREEN,可是当我传入该参数后,结果是:只是状态栏消失了,但是位置还在。
喜欢我,就点我吧!