Android:从首选项中选择一个主题后,如何将主题应用于应用程序的所有活动? [英] Android: How to I apply a theme to all the activities of application after selecting one from preferences?

查看:53
本文介绍了Android:从首选项中选择一个主题后,如何将主题应用于应用程序的所有活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个简单的应用程序,我想在从首选项中选择后更改应用程序所有活动(包括设置活动)的主题,但选择后主题不适用.我试过添加一个 recreate() 但在尝试开始主要活动时遇到了困难.现在,我添加了一个 recreate() 菜单项,但我希望它在您离开设置活动后自动应用.

I have made a simple app and i want to change the theme of all the activities of the app (including the settings activity) after selecting from the preference but the themes do not apply after selection. I've tried adding a recreate() but gets stuck in trying to start the main activity. For now, i added a menu item that does recreate() but i want it to apply automatically once you leave the settings activity.

MainActivity.java:

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences getData = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String themeValues = getData.getString("theme_preference", "1");

    if (themeValues.equals("1")) {
        setTheme(R.style.Theme_Light);
    }

    if (themeValues.equals("2")) {
        setTheme(R.style.Theme_Dark);
    }

    if (themeValues.equals("3")) {
        setTheme(R.style.Theme_Red);
    }

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
}

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (id == R.id.action_settings) {
            Intent i = new Intent("com.cyanoise.helloworld.SettingsActivity");
            startActivity(i);

        return true;
    }

    if (id == R.id.about_app) {
        startActivity(new Intent(MainActivity.this, AboutActivity.class));

        return true;
    }

    if (id == R.id.refresh_app) {
        recreate();

        return true;
    }

    if (id == R.id.exitapp) {
        finish();
    }

    return super.onOptionsItemSelected(item);
}

SettingsActivity.java:

SettingsActivity.java:

public class SettingsActivity extends PreferenceActivity {

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

        addPreferencesFromResource(R.xml.settings);
    }
}

非常感谢所有答案.

推荐答案

好的,所以我一直在玩,并找到了解决方法.我在 SettingsActivity 中添加了一个 onBackPressed() 以再次启动 MainActivity 并且它似乎可以工作,退出设置活动后应用所选主题.

Okay so I've been playing around and found a workaround to it. I added a onBackPressed() in the SettingsActivity to bring up the MainActivity again and it seemed to work, applying the selected theme once exiting the settings activity.

@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(SettingsActivity.this, MainActivity.class));
}

至于将主题应用于我刚刚添加的所有活动

As for applying themes to all activities i just added

SharedPreferences getData = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String themeValues = getData.getString("theme_preference", "1");

    if (themeValues.equals("1")) {
        setTheme(R.style.Theme_Light);
    }

    if (themeValues.equals("2")) {
        setTheme(R.style.Theme_Dark);
    }

    if (themeValues.equals("3")) {
        setTheme(R.style.Theme_Red);
    }

到每个活动的每个 onCreate.

to every onCreate of every activity.

这篇关于Android:从首选项中选择一个主题后,如何将主题应用于应用程序的所有活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆