2013-02-22 76 views
2

我一直試圖讓我的應用程序從首選項中讀取數據,並根據所選的選項更改主題。我在互聯網上發現了許多不同的建議,包括這裏,但一直未能得到它的工作。使用偏好更改Android應用程序的主題

我已經創建了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); 
} 

任何幫助,將不勝感激。

回答

3

setTheme()僅在之前生效佈局已經構建,即您應該在setContentView()之前調用它。 LayoutInflater解決了主題屬性,並據此設置了它創建的View的屬性。要在已經運行的活動中應用主題,您必須重新啓動活動。

+0

setTheme()在setContentView()之前是不是? 要重新啓動活動,是否需要將recreate()放在onResume部分中? – 2013-02-22 13:45:12

+0

@CarlAnsell使'setTheme()'成爲'onCreate()'中的第一條語句。 http://code.google.com/p/android/issues/detail?id=4394 – 2013-02-22 13:46:33

+0

謝謝,它現在正在工作。但是,它只在按下應用程序後退按鈕時才起作用,而不是系統後退按鈕。我更新了我在OP中使用的代碼。 – 2013-02-22 14:13:08

-1

我知道我遲到了,但我想在這裏發表一個解決方案:
檢查完整的源代碼here.
這是我使用的偏好變化的主題時使用的代碼..

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之前放置代碼..

+0

請不要在多個問題上發佈相同的答案。如果問題重複,則發佈最佳問題的答案,並將其他問題標記爲最佳問題的副本。如果它們不是重複的,那麼你需要調整每一個答案來回答這個具體的問題。此外,請嘗試在您的每一個答案中獲得格式化權限和[不包括](http://meta.stackexchange.com/q/2950/266187)「快樂編碼」。 – 2015-10-24 16:42:24

相關問題