如何从另一个活动设置主活动的主题?

3

我有两个活动,一个是主活动,另一个是设置活动。

我希望在用户从颜色选择对话框中选择颜色时,能够从设置活动中更改主题。

目前,我能够更改主题,但这只更改了设置活动的主题,而不是主活动。我该如何将主题设置为主活动?

设置活动:

public class Settings extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {


          Theme.onActivityCreateSetTheme(this);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);



    final ColorPickerDialog colorPickerDialog = new ColorPickerDialog();
    colorPickerDialog.initialize(R.string.dialog_title, new int[]{Color.CYAN, Color.LTGRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED, Color.GRAY, Color.YELLOW}, Color.YELLOW, 3, 2);
    colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() {

        @Override
        public void onColorSelected(int color) {



            //  startActivityForResult(new Intent(getApplicationContext(),
            //         ThemedPreferenceActivity.class), SETTINGS_ACTION);

            if (color == Color.CYAN) {
                Theme.changeToTheme(Settings.this, Theme.THEME_DEFAULT);
                no = 0;
            }
            else if (color == Color.LTGRAY)

            {
                Theme.changeToTheme(Settings.this, Theme.THEME_WHITE);
                no = 1;
            }
            else if (color == Color.BLACK) {

                Theme.changeToTheme(Settings.this,Theme.THEME_BLUE);
                no = 3;

            }
            Toast.makeText(Settings.this, "selectedColor : " + color, Toast.LENGTH_SHORT).show();
        }

    });



    SharedPreferences.Editor editor = sharedpreferences.edit();

    editor.putInt("color_1",no);
    editor.putInt("color_2",no);
    editor.putInt("color_3",no);
    editor.commit();

    LinearLayout theme = (LinearLayout)findViewById(R.id.theme);

    theme.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            colorPickerDialog.show(getSupportFragmentManager(), "colorpicker");
        }
    });


}

主题:

 public class Theme {

    private static int sTheme;

    public final static int THEME_DEFAULT = 0;
    public final static int THEME_WHITE = 1;
    public final static int THEME_BLUE = 2;

    /** * Set the theme of the Activity, and restart it by creating a new Activity of the same type. */
    public static void changeToTheme(Activity activity, int theme) {
        sTheme = theme; activity.finish();

  activity.startActivity(new Intent(activity, activity.getClass()));
    }

    /** Set the theme of the activity, according to the configuration. */
    public static void onActivityCreateSetTheme(Activity activity) {
        switch (sTheme) {
            default: case THEME_DEFAULT:
                activity.setTheme(R.style.BlueTheme);
                break;
        }
    }
}

编辑:

主活动:

  @Override
protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();


    no = pref.getInt("color_1", 0);        
    no = pref.getInt("color_2", 0);
    no = pref.getInt("color_3", 0);

    if(no == 0)
    {
        setTheme(R.style.AppTheme);
    }

    else if(no == 1)
    {
        setTheme(R.style.AppTheme_Solarized);
    }

    else if(no == 2)
    {
        setTheme(R.style.BlueTheme);
    }


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

谢谢您。

如何在Android中运行时更改当前主题 - user5383419
2个回答

0
     public void updateTheme(int themeID) {
        setTheme(themeID);
         Intent intent = getIntent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
        overridePendingTransition(0, 0);
    }

当用户从颜色选择对话框中选择颜色时,我想动态更改主题。这已经在清单中设置好了。@ahmad aghazadeh - user5881997
如何在哪里使用这个方法? - user5881997

0

除非重新启动当前活动,否则无法动态更改主题。

您可以使用两种方法来实现您的问题:

  1. 在设置内容视图之前,在 activity 的 onCreate 中检查共享偏好数据并相应地设置主题。从“设置”活动中设置共享数据,在“MainActivity”中检查共享数据。

    public void onCreate(Bundle savedInstanceState)
    {
        if (checkColorData("navcolor").equals("SomeColor"))
        {
            setTheme(R.style.AppTheme);
        }
        else
        {
            setTheme(R.style.AppThemeDarkNav);
        }
    }
    
  2. 使用工具栏作为操作栏,并使用一些可更改颜色的视图,您可以通过调用 setBackgoundColor 更改颜色。


我尝试了这个,但主活动仍然没有变化。请你能否检查一下修改后的问题并告诉我出了什么问题?@Flygenring, @Bharath Kumar - user5881997

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接