使用偏好设置更改Android应用程序的主题

3

我一直在尝试让我的应用程序从首选项中读取数据,并根据所选选项更改主题。我在互联网上找到了许多不同的建议,包括这里,但是我无法使它工作。

我创建了preferences.xml和arrays.xml,用户可以选择他们想要的主题。然而,在应用程序中没有反映出来。

以下是ActivityMain.java的内容:

public class MainActivity extends Activity {

 protected void onCreate(Bundle savedInstanceState) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String userTheme = preferences.getString("prefTheme", "darkab"); 
    if (userTheme.equals("darkab"))
        setTheme(R.style.darkab);
    else if (userTheme.equals("light"))
        setTheme(R.style.light);
    else if (userTheme.equals("dark"))
        setTheme(R.style.dark);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@SuppressLint("NewApi")
protected void onResume(Bundle savedInstanceState) {
    recreate();
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String userTheme = preferences.getString("prefTheme", "darkab");
    if (userTheme.equals("darkab"))
        setTheme(R.style.darkab);
    else if (userTheme.equals("light"))
        setTheme(R.style.light);
    else {setTheme(R.style.dark);}
    super.onResume();
    setContentView(R.layout.activity_main);
}

这些是我希望使用的样式,已在styles.xml中设置:

<style name="darkab" parent="android:Theme.Holo.Light.DarkActionBar"></style>
<style name="light" parent="android:Theme.Holo.Light"></style>
<style name="dark" parent="android:Theme.Holo">

以下是我的preferences.java文件:

public class Preferences extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
}

非常感谢您的帮助。

2个回答

4

setTheme() 只有在布局被构建之前才有效,即你应该在调用 setContentView() 之前调用它。 LayoutInflater 解析主题属性,并相应地设置创建的 View 的属性。要在已经运行的 Activity 上应用主题,您必须重新启动 Activity。


setTheme() 在 setContentView() 之前,对吧?要重新启动活动,我需要在 onResume 部分放置 recreate() 吗? - Carl Ansell
@CarlAnsell 在 onCreate() 中将 setTheme() 设为第一条语句。http://code.google.com/p/android/issues/detail?id=4394 - S.D.
谢谢,现在它可以工作了。但是,只有在按应用内返回按钮时才有效,而不是系统返回按钮。我已经更新了我在原帖中使用的代码。 - Carl Ansell

-1

我知道我来晚了,但我想在这里发布一个解决方案:
请查看完整的源代码此处。
这是我在使用首选项更改主题时使用的代码...

SharedPreferences pref = PreferenceManager
            .getDefaultSharedPreferences(this);
String themeName = pref.getString("prefSyncFrequency3", "Theme1");
if (themeName.equals("Africa")) {
    setTheme(R.style.AppTheme);
} else if (themeName.equals("Colorful Beach")) {
    //Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
    setTheme(R.style.beach);
} else if (themeName.equals("Abstract")) {
    //Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
    setTheme(R.style.abstract2);
} else if (themeName.equals("Default")) {
    setTheme(R.style.defaulttheme);
}

请注意,在setcontentview之前放置代码。

请勿在多个问题中发布相同的答案。如果这些问题是重复的,则在最佳问题下发布答案,并将其他问题标记为最佳问题的重复项。如果它们不是重复的,则需要调整每个答案以回答该特定问题。此外,请尝试正确格式化,并且在您的每个答案中都不要包含“HAPPY CODING”。 - Artjom B.

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