如何在应用设置中更改主题

时间:2015-04-14 21:09:50

标签: java android settings oncreate onpause

我正在开发一个应用程序,我希望用户能够从设置中的颜色选项列表中选择(所有代码如下所示)。但是,当他们这样做时,它不会在您设置活动后更改。当你离开活动(比如回家)并回来时它会改变。我相信这是因为onCreate()没有被调用,问题是如果我再次setContentView(),整个应用程序都没有响应。如果我recreate()它因音乐播放器而崩溃(再次下面的代码)。即使您认为它会起作用,请留下答案。提前谢谢。

代码: SettingsActivity.java(也无法在此更改栏)

    public class SettingsActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String themeName = settings.getString("theme", "DarkBlue");
        int theme = MyThemer.getThemeid(themeName);
        if(Build.VERSION.SDK_INT > 20){
            int themeDark = MyThemer.getColorPrimaryDark(themeName);
            getWindow().setStatusBarColor(getResources().getColor(themeDark));
        }
        setTheme(theme);
        addPreferencesFromResource(R.layout.settings_activity);

        String themName = settings.getString("theme", "DarkBlue");
        int barColor = MyThemer.getColorPrimary(themName);

        LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
        Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
        bar.setBackgroundColor(getResources().getColor(barColor));
        root.addView(bar, 0); // insert at top
        bar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

代码: MainActivity.java

的onCreate()

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    mySetTheme();
    setContentView(R.layout.activity_game_over);

setTheme()

private void mySetTheme(){
    themeName = settings.getString("theme", "DarkBlue");
    int theme = MyThemer.getThemeid(themeName);
    setTheme(theme);
    if(Build.VERSION.SDK_INT > 20){
        int themeDark = MyThemer.getColorPrimaryDark(themeName);
        getWindow().setStatusBarColor(getResources().getColor(themeDark));
    }
}

的onResume()

public void onResume(){
    super.onResume();
    playMusic = settings.getBoolean("playMusic", true);
    if(playMusic){
        backgroundMusicPlayer = new BackgroundMusicPlayer();
        backgroundMusicPlayer.execute(getApplicationContext());
    }
}

的onPause()

public void onPause(){
    super.onPause();
    if(playMusic){
        backgroundMusicPlayer.cancelMusic();
        backgroundMusicPlayer.cancel(true);
    }
}

backgroundMusicPlayer.java

public class BackgroundMusicPlayer extends AsyncTask<Context, Void, Void>{

    MediaPlayer player;
    @Override
    protected Void doInBackground(Context... params) {
        player = MediaPlayer.create(params[0], R.raw.background);
        player.setLooping(true);
        player.setVolume(50,50);
        player.start();
        return null;
    }

    public void cancelMusic(){
        player.stop();
        player.reset();
        player.release();
    }
}

1 个答案:

答案 0 :(得分:0)

只需要用

替换finish();
Intent home = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(home);

非常感谢您阅读我的问题。