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

时间:2018-05-26 17:38:08

标签: android themes settings diacritics light

我试图为应用程序构建设置,我想让它可自定义,让用户在明暗主题之间做出决定。我已经按照本教程完成了这个https://www.hidroh.com/2015/02/16/support-multiple-themes-android-app/

问题是,我想要另一种方法来选择强调色,就像许多应用程序一样。我如何独立于黑暗/光线实现这一目标?有没有办法避免重启活动?

1 个答案:

答案 0 :(得分:0)

我使用的方法是,将颜色保留在数组中,让用户选择颜色并在首选项中存储所选颜色的索引。因此,一旦加载活动时,读取存储的颜色并相应地设置颜色

    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)));
    }

main screen where they select the book

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);

    }

}