如何从Android studio中的颜色微调器设置默认主题?

时间:2017-03-08 11:28:22

标签: android android-studio android-spinner android-theme

我制作一个微调器来选择主题颜色,它可以工作并设置活动主题 但是当关闭应用程序并再次打开它时,默认主题显示不是最后一个选择,这个我的代码可以让任何人告诉我我能改变什么

MainActivity.java

public class MainActivity extends AppCompatActivity {

final ArrayList<Integer> colors = new ArrayList<Integer>() {{

            add(R.color.colorPrimaryDark);
            add(R.color.redPrimaryDark);
            add(R.color.pinkPrimaryDark);
            add(R.color.greenPrimaryDark);
            add(R.color.tealPrimaryDark);
            add(R.color.purplePrimaryDark);
            add(R.color.lightBluePrimaryDark);
            add(R.color.yellowPrimaryDark);
            add(R.color.orangePrimaryDark);
            add(R.color.brownPrimaryDark);
            add(R.color.greyPrimaryDark);
}};

private Spinner Colors_Spinner;
private int Colors_currentSelectedItemPosition = 0;


final Integer colors2[] = {
        R.color.colorPrimaryDark,
        R.color.redPrimaryDark,
        R.color.pinkPrimaryDark,
        R.color.greenPrimaryDark,
        R.color.tealPrimaryDark,
        R.color.purplePrimaryDark,
        R.color.lightBluePrimaryDark,
        R.color.yellowPrimaryDark,
        R.color.orangePrimaryDark,
        R.color.brownPrimaryDark,
        R.color.greyPrimaryDark};

CustomSpinnerAdapter customSpinnerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    // MUST BE SET BEFORE setContentView
    Utils.onActivityCreateSetTheme(this);
    setContentView(R.layout.activity_main);


    customSpinnerAdapter = new CustomSpinnerAdapter(this, android.R.layout.simple_spinner_dropdown_item, colors2);
    Colors_Spinner = (Spinner) findViewById(R.id.Colors_Spinner);

    Initialize_UI();
}

private void Initialize_UI() {

    Colors_Spinner.setAdapter(customSpinnerAdapter);
    Colors_Spinner.setSelection(0);
    Colors_Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

           Colors_Spinner.setSelection( colors.indexOf(colors.get(position)));

            if ( Colors_currentSelectedItemPosition != position) {
                Utils.changeToTheme(MainActivity.this, position);
            }

            Colors_currentSelectedItemPosition = position;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}

Utils.java

public class Utils
    {
         private static int sTheme;
        public final static int THEME_Blue = 0;
        public final static int THEME_RED = 1;
        public final static int THEME_PINK =2;
        public final static int THEME_GREEN =3;
        public final static int THEME_TEAL =4;
        public final static int THEME_PURPLE =5;
        public final static int THEME_LIGHTBLUE =6;
        public final static int THEME_YELLOW =7;
        public final static int THEME_ORANGE = 8;
        public final static int THEME_BROWN =9;
        public final static int THEME_GREY =10;


        public static void changeToTheme(Activity activity, int theme) {

            sTheme = theme;
            activity.finish();
            activity.startActivity(new Intent(activity, activity.getClass()));
            activity.overridePendingTransition(android.R.anim.fade_in,
                    android.R.anim.fade_out);
        }
        public static void onActivityCreateSetTheme(Activity activity) {

            switch (sTheme) {
                default:
                case THEME_Blue:
                    activity.setTheme(R.style.BlueTheme);
                    break;
                case THEME_RED:
                    activity.setTheme(R.style.RedTheme);
                    break;
                case THEME_PINK:
                    activity.setTheme(R.style.PinkTheme);
                    break;
                case THEME_PURPLE:
                    activity.setTheme(R.style.PurpleTheme);
                    break;
                case THEME_TEAL:
                    activity.setTheme(R.style.TealTheme);
                    break;
                case THEME_LIGHTBLUE:
                    activity.setTheme(R.style.LightBlueTheme);
                    break;
                case THEME_GREEN:
                    activity.setTheme(R.style.GreenTheme);
                    break;
                case THEME_YELLOW:
                    activity.setTheme(R.style.YellowTheme);
                    break;
                case THEME_ORANGE:
                    activity.setTheme(R.style.OrangeTheme);
                    break;
                case THEME_BROWN:
                    activity.setTheme(R.style.BrownTheme);
                    break;
                case THEME_GREY:
                    activity.setTheme(R.style.GreyTheme);
                    break;
            }
        }

CustomSpinnerAdapter.java

public class CustomSpinnerAdapter extends ArrayAdapter<Integer>
{
     private Context mContext;
    private Integer  [] mColors;

    public CustomSpinnerAdapter(Context context, int resource, Integer [] Colors) {
        super(context, resource,Colors);
        mContext=context;
        mColors=Colors;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
        TextView circle;
         if (convertView == null) {
            convertView = inflater.inflate(R.layout.color_spinner_row, parent, false);
            circle = (TextView) convertView.findViewById(R.id.circle);
            convertView.setTag(circle);
          } else
            circle = (TextView) convertView.getTag();

        // Do everything you want with  "circle"
        GradientDrawable bgShape = (GradientDrawable)circle.getBackground();
        bgShape.setColor(mContext.getResources().getColor(mColors[position]));

        return convertView;
    }

1 个答案:

答案 0 :(得分:0)

当您重新启动应用程序时,使用的默认主题来自标记内“android:theme”行中的Manifest.xml。 如果您想要选择主题的持久性,则必须将其保存在SharedPreferences(https://developer.android.com/reference/android/content/SharedPreferences.html)中。