How to programmatically change the primary color in Android L?

后端 未结 4 464
别跟我提以往
别跟我提以往 2020-12-24 14:11

Is there a way to change programmatically the primary colors. I would like to do it in code depending on the screen/state of the app.

Currently I can only set the co

4条回答
  •  鱼传尺愫
    2020-12-24 14:50

    USe this code for setting toolbarcolor and status bar (darker toolbar color)

    toolbar.setBackgroundColor(toolbarColor);
    factor=0.8f; 
    int a = Color.alpha(toolbarcolor);
    int r = Math.round(Color.red(toolbarcolor) * factor);
    int g = Math.round(Color.green(toolbarcolor) * factor);
    int b = Math.round(Color.blue(toolbarcolor) * factor);
    int statusColor=Color.argb(a,
            Math.min(r, 255),
            Math.min(g, 255),
            Math.min(b, 255));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = MainActivity.this.getWindow();
        window.setStatusBarColor(statusColor);
    }
    

提交回复
热议问题