在ios的所有应用中系统栏和应用在风格和色彩上都非常统一,显得非常唯美。那么安卓是否也能实现相同的效果呢?答案是肯定的。在安卓4.4(API19)以后系统开始支持沉浸效果,MIUI通过Xposed框架可以实现非系统应用的沉浸效果,安卓官方也提供了了两套系统的主题可以实现NoActionBar的沉浸,可以再style.xml进行更改尝试,在开发中我们也可以通过代码实现应用在4.4以上不同ROM中的沉浸效果,下来介绍两种方法(两种方法均需要一个第三方jar包,将其放入lib文件夹中,下载地址:点击打开链接):
方法一:
1、在stylex.xml中你所引用的主题style<>中加入
- <!-- Status Bar -->
- <item name="android:windowTranslucentStatus">true</item>
- <!-- Navigation Bar -->
- <item name="android:windowTranslucentNavigation">true</item>
2、在MainActivity.java的onCreate()方法中进行设置
-
- getActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(51, 181, 250)));
-
- <span style="white-space:pre"> </span>SystemBarTintManager tintManager = new SystemBarTintManager(this);
-
- tintManager.setStatusBarTintEnabled(true);
-
- tintManager.setNavigationBarTintEnabled(true);
-
- tintManager.setTintColor(Color.rgb(51, 181, 250));
方法二:
直接在MainActivity.java的onCreate()方法中添加
-
- getActionBar().setBackgroundDrawable(
- new ColorDrawable(Color.rgb(51, 181, 250)));
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- setTranslucentStatus(true);
- }
-
- SystemBarTintManager tintManager = new SystemBarTintManager(this);
-
- tintManager.setStatusBarTintEnabled(true);
- tintManager.setTintColor(Color.rgb(51, 181, 250));
- }
-
- @TargetApi(19)
- private 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;
- } else {
- winParams.flags &= ~bits;
- }
- win.setAttributes(winParams);
- }
最终实现的效果如下:
