简体   繁体   中英

Android modify StatusBar Color

I set up a project and want to work with Material design. As the app should be compatible with APIs lower than 21, I use appcompat. Unfortunately I cannot change the color of the status bar.

I already read: How to change the status bar color in android And: https://chris.banes.me/2014/10/17/appcompat-v21/

My Layout is android.support.v4.widget.DrawerLayout. The color of the ActionBar changes, but not of the status bar. My xml:

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.


    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/green500</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/green900</item>
</style>

Actually, the app can't change the status bar color with lower than API 19.

I wrote a demo. It works fine when API level higher or equal API 19. Your style setting is right. But if you want the app be compatible with API 19. You should add some codes.

class Utils {
private static int getStatusBarHeight(Context context) {
    Context appContext = context.getApplicationContext();
    int result = 0;
    int resourceId = appContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = appContext.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

static void setStatusbarColor(Activity activity, View view) {
    Window w = activity.getWindow();
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
        w.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        w.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        int statusBarHeight = getStatusBarHeight(activity);
        view.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT
                , view.getLayoutParams().height + statusBarHeight));
        view.setPadding(0, statusBarHeight, 0, 0);
    }
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    Utils.setStatusbarColor(this, toolbar);
    setSupportActionBar(toolbar);
}

if you want to check all codes. Please go to here: https://github.com/SawDouma/StatusBarColorChanged/tree/master

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM