让用户设置浅色或深色主题加上强调色

Let the user set light or dark theme plus accent color

我正在尝试为应用程序构建设置,我想让它可以自定义,让用户在浅色和深色主题之间做出选择。我已经按照本教程完成了 https://www.hidroh.com/2015/02/16/support-multiple-themes-android-app/

问题是,我想要 select 重音颜色的另一个选项,就像许多应用程序一样。我怎样才能独立于 dark/light 实现这一点?有没有办法避免 activity 重启?

我使用的方法是,将颜色保存在一个数组中,让用户 select 颜色并将 selected 颜色的索引存储在首选项中。因此,当 activity 正在加载时,读取存储的颜色并相应地设置颜色

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this,colors.getResourceId(chapter_key-1, 0)));
    }

package com.**********.app.customViews;

/**
 * Created by pakistantechhouse on 18/02/2017.
 */

import android.content.Context;
import android.graphics.Canvas;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.**********.app.R;


public class CRelativeLayout extends RelativeLayout {


    public CRelativeLayout(Context context) {
        super(context);
        if (!isInEditMode()) {
            //TODO get your color here from the preferences and apply to the view
            this.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
        }
    }

    public CRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode()) {
            //TODO get your color here from the preferences and apply to the view 
            this.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));

        }
    }

    public CRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (!isInEditMode()) {
            //TODO get your color here from the preferences and apply to the view 

            this.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
        }
    }

    protected void onDraw (Canvas canvas) {
        super.onDraw(canvas);

    }

}