改变状态栏颜色

来源:互联网 发布:mac app store下载慢 编辑:程序博客网 时间:2024/05/16 08:15

这样的效果:改变状态栏颜色

方法一.使用SystemBarTintManager :
在需要改变状态栏颜色的Activity中加入代码:

 //只对API19以上版本有效        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            setTranslucentStatus(true);        }        //为状态栏着色        SystemBarTintManager tintManager = new SystemBarTintManager(this);        tintManager.setStatusBarTintEnabled(true);        tintManager.setStatusBarTintResource(R.color.statusbar);
  @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);    }

方法二:
1.先在values文件夹下的styles.xml 文件中设置如下主题:

   <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>    </style>    <!--在Android 4.4之前的版本上运行,直接跟随系统主题-->    <style name="TranslucentTheme" parent="AppTheme">    </style>

2.在values-19中添加styles:

    <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">        <item name="android:windowTranslucentStatus">true</item>        <item name="android:windowTranslucentNavigation">false</item>    </style>

3.在values-21中添加styles:

  <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">        <item name="android:windowTranslucentStatus">true</item>        <item name="android:windowTranslucentNavigation">false</item>        <item name="android:statusBarColor">@android:color/transparent</item>    </style>

4.在相应的activity中添加AppTheme:

 android:theme="@style/AppTheme"

5.在xml文件最外层布局中添加:

 android:fitsSystemWindows="true"

这样的效果
隐藏状态栏
在Activity中添加:

      View decorView = getWindow().getDecorView();        int option = View.SYSTEM_UI_FLAG_FULLSCREEN;        decorView.setSystemUiVisibility(option);        ActionBar actionBar = getSupportActionBar();        actionBar.hide();

这样的效果(5.0以上):
沉浸式

 if (Build.VERSION.SDK_INT >= 21) {            View decorView = getWindow().getDecorView();            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;            decorView.setSystemUiVisibility(option);            getWindow().setStatusBarColor(Color.TRANSPARENT);        }        ActionBar actionBar = getSupportActionBar();        actionBar.hide();    }
0 0
原创粉丝点击