先看几张图对比一下在不同版本的显示效果 4.4沉浸模式: 6.0沉浸模式: 4.4状态栏颜色: 6.0状态栏颜色: 下面看实现过程: 首先去github上下载SystemBarTint源码 SystemBarTint 下载后发现其中只有一个关键类SystemBarTintManager,将它复制到自己的项目中。 修改SystemBarTintManager类中DEFAULT_TINT_COLOR的值,这个是默认添加在状态栏位置的布局的颜色
/** * The default system bar tint color value. */ // 将颜色变为透明public static final int DEFAULT_TINT_COLOR = 0x00000000;styles.xml代码
<resources> <!-- Base application theme. --> <style name="APPTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPRimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="FullBleedTheme" parent="AppTheme"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowContentOverlay">@null</item> </style> <style name="ActionBarTheme" parent="AppTheme"> <item name="android:clipToPadding">true</item> <item name="android:fitsSystemWindows">true</item> </style></resources>定义Activity样式
<activity android:name=".ActionbarActivity" android:theme="@style/ActionBarTheme"></activity><activity android:name=".FullscreenActivity" android:theme="@style/FullBleedTheme"></activity>沉浸模式Activity代码
public class FullscreenActivity extends AppCompatActivity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fullscreen); Toolbar myToolbar = (Toolbar) findViewById(R.id.toolBar); setSupportActionBar(myToolbar); // create our manager instance after the content view is set SystemBarTintManager tintManager = new SystemBarTintManager(this); // enable status bar tint tintManager.setStatusBarTintEnabled(true); // enable navigation bar tint tintManager.setNavigationBarTintEnabled(true); }}设置状态栏颜色代码
public class ActionbarActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fullscreen); Toolbar myToolbar = (Toolbar) findViewById(R.id.toolBar); setSupportActionBar(myToolbar); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(true); } SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(R.color.colorPrimary); } @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); }}总结:SystemBarTint在4.4跟6.0的沉浸模式效果还不错,同时能够修改4.4系统的状态栏颜色,不过在6.0的手机上修改状态栏颜色效果不是太好(可以在主题中直接修改状态栏颜色)。
新闻热点
疑难解答