Android两行代码修改给系统状态栏设置透明效果
16lz
2021-01-25
在实际开发中为了提高整体的视觉,有时候会把系统栏设置为自定义的颜色。或者跟随系统。
效果图
1、Android中给我们提供的有方法
比如:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } //透明状态栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明导航栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
在设置完View布局之后调用该方法,就可以实现状态栏的改变,但是运行会发现,这样把整体的布局都上移了。相当于inVisible和Gone的区别。
但是要实现只隐藏的效果还需要在xml根布局的父控件中添加一行
android:fitsSystemWindows="true"
这样虽然可以实现同样的效果,但是每建一个Activity都需要拷贝大量的代码。不利于查看。
2、也可以使用一行代码搞定
这个是我们当前项目中使用的方法。
X_SystemBarUI.initSystemBar(this,R.color.colorPrimary);
调用该方法需要定义两个类
SystemBarTintManager.javaX_SystemBarUI.java
部分代码
/** * 改变系统标题栏颜色 * @param activity * @param color color xml文件下的颜色 */ public static void initSystemBar(Activity activity, int color) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(activity, true); } SystemBarTintManager tintManager = new SystemBarTintManager(activity); tintManager.setStatusBarTintEnabled(true); // 使用颜色资源 tintManager.setStatusBarTintResource(color); } /** * 设置系统标题栏的透明度 * @param activity * @param on */ @TargetApi(19) private static void setTranslucentStatus(Activity activity, boolean on) { Window win = activity.getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); }
3、这个是网上找的一个方法
new SystemStatusManager(this).setTranslucentStatus(R.color.colorPrimary);
只需要定义一个类:
SystemStatusManager.java
部分代码:
public void setTranslucentStatus(int res) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // 透明状态栏 this.mContext.getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 透明导航栏 this.mContext.getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); SystemStatusManager tintManager = new SystemStatusManager(this.mContext); tintManager.setStatusBarTintEnabled(true); // 设置状态栏的颜色 tintManager.setStatusBarTintResource(res); this.mContext.getWindow().getDecorView().setFitsSystemWindows(true); } } static { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { try { Class c = Class.forName("android.os.SystemProperties"); Method m = c.getDeclaredMethod("get", String.class); m.setAccessible(true); sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys"); } catch (Throwable e) { sNavBarOverride = null; } } }
这个方法中还有一个去除标题栏和状态栏之间黑线的功能
具体使用:在appTheme主题中加入:
- @null
这三个类都被我放在了github上,直接传送过去复制就行
源码地址
最后别忘了在父控件中添加一行
android:fitsSystemWindows="true"
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- Qt for Android(安卓)环境搭建方法
- 跨平台移动开发_Android(安卓)平台使用 PhoneGap 方法
- [置顶] Retrofit2使用方式和源码解析
- Android开发笔记: Project "XXX" is missing required source fo
- android 解决依赖冲突
- [Android] Smali基本语法
- 我的android——OpenGL(2)——gl10方法解析
- Android培训班(29)