2015-05-14 87 views
1

Hello我在styles.xml中有四個自定義的樣式,我嘗試使用RadioGroup來選擇整個應用程序的總體樣式。使用RadioButtons更改整個Android應用程序的主題

<style name="blueTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen"> 
    <item name="android:windowBackground">@color/blue_background</item> 
    <item name="android:textColorPrimary">@color/blue_text</item> 
</style> 
<style name="redTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen"> 
    <item name="android:windowBackground">@color/red_background</item> 
    <item name="android:textColorPrimary">@color/red_text</item> 
</style> 
<style name="greenTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen"> 
    <item name="android:windowBackground">@color/green_background</item> 
    <item name="android:textColorPrimary">@color/green_text</item> 
</style> 
<style name="bwTheme" parent="android:Theme.Light.NoTitleBar.Fullscreen"> 
    <item name="android:windowBackground">@color/bw_background</item> 
    <item name="android:textColorPrimary">@color/bw_text</item> 
</style> 

這裏是我的實際的RadioGroup中選擇的處理的代碼,我以前用的CheckBox嘗試這個,這就是爲什麼名稱是所有複選框。

blueCheckBox = (RadioButton) findViewById(R.id.blueCheckBox); 
    redCheckBox = (RadioButton) findViewById(R.id.redCheckBox); 
    greenCheckBox = (RadioButton) findViewById(R.id.greenCheckBox); 
    bwCheckBox = (RadioButton) findViewById(R.id.bwCheckBox); 

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.themeRadioGroup); 
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
    { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      // checkedId is the RadioButton selected 

      switch(checkedId) { 
       case R.id.blueCheckBox: 
         getApplication().setTheme(R.style.blueTheme); 
        break; 
       case R.id.redCheckBox: 
        getApplication().setTheme(R.style.redTheme); 
        break; 
       case R.id.greenCheckBox: 
        getApplication().setTheme(R.style.greenTheme); 
        break; 
       case R.id.bwCheckBox: 
        getApplication().setTheme(R.style.bwTheme); 
        break; 
      } 
     } 
    }); 

我遇到的問題是整體風格根本不會改變。在我的AndroidManifest.xml文件中,我默認我的主題爲

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/blueTheme" > 

但儘管如此,當我運行我的應用程序主題不是blueTheme。 blueTheme沒有當前所有應用程序的活動顯示的顏色。 由於某些原因,顯示的主題具有灰色文本和深灰色/黑色背景,正確顯示的唯一顏色是我在各個佈局/ activity_myActiviitesThatHaveButtons xml文件中手動設置的按鈕的背景顏色。我不確定我做錯了什麼,任何人都可以請嘗試提供一些幫助?感謝您的幫助

P.S:不知道這個問題是否重要,但我相信我的min API設置爲8,我相信這是我的教授所希望的。

回答

0

創建一個覆蓋onCreate並基於首選項設置活動主題的基本活動。然後讓所有的活動重寫此首選項。當用戶在單選按鈕上改變他們的選擇時,首選項將被設置。

的更多信息可以在類似的問題中找到解答之前:

Switching application-wide theme programmatically?

相關問題