0

每次用户加载某个Activity.

我创建了一种在 3 个主题之间切换的方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRandomTheme();
    setContentView(R.layout.activity_main);
}

private void setRandomTheme() {
    int NUMBER_OF_THEMES = 3;
    int theme_ID = (int) (Math.random() * NUMBER_OF_THEMES);
    setTheme(Provider.getTheme(theme_ID));
}

但是我必须在AndroidManifest.xmlandroid:theme中声明一个与 Activity 相关的内容,所以我不能像那样简单地切换主题。

有没有办法实现它?

4

1 回答 1

0

在 super.onCreate 之前设置主题

@Override
public void onCreate(Bundle savedInstanceState) {
    setRandomTheme();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

private void setRandomTheme() {
    int NUMBER_OF_THEMES = 3;
    int theme_ID = (int) (Math.random() * NUMBER_OF_THEMES);
    setTheme(Provider.getTheme(theme_ID));
}
于 2013-09-07T00:12:33.270 回答