设置statusbar的颜色,适用于SDK4.4版本及以上版本

来源:互联网 发布:originlab mac 破解 编辑:程序博客网 时间:2024/06/06 02:33

Android应用,自定义statusbar的颜色

1、效果图

这里写图片描述

2、方法

为了方便调用,你可以将此方法封装在BaseActivity中。

protected void setStatusBarBackground() {        int color = Color.BLUE;        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {            color = getResources().getColor(R.color.colorPrimary, getTheme());        } else {            color = getResources().getColor(R.color.colorPrimary);        }        setStatusBarBackground(color);    }    /**     * <p>设置statusBar的背景颜色。     * <br>在setContentView()方法之后调用。     * <br>SDK4.4以下版本无效。     * @param background     */    protected void setStatusBarBackground(int background) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);            }            //设置statusBar的颜色            getWindow().setStatusBarColor(background);        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){            // 1.设置状态栏透明,经测试在代码里直接声明透明状态栏更有效            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);            View statusBarView = contentView.getChildAt(0);            //改变颜色时避免重复添加statusBarView            int statusbarHeight = getStatusBarHeight();            if (statusBarView == null || statusBarView.getMeasuredHeight() != statusbarHeight) {                statusBarView = new View(this);            }            statusBarView.setBackgroundColor(background);            contentView.addView(statusBarView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusbarHeight));        }    }

3、使用

values文件夹下的styles.xml

<resources>    <!-- 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>    <style name="AppTheme.NoActionBar"/>    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /></resourcs>

values-v19文件夹下的styles.xml

<resources>    <style name="AppTheme.NoActionBar">        <item name="android:windowTranslucentStatus">true</item>    </style></resources>

values-v21文件夹下的styles.xml

 <resources>    <style name="AppTheme.NoActionBar">        <item name="android:windowDrawsSystemBarBackgrounds">true</item>        <item name="android:statusBarColor">@android:color/transparent</item>    </style></resources>
@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_input_password);        setStatusBarBackground();        //do your job    }
0 0
原创粉丝点击