2016-07-29 29 views
4

我想製作一個SettingsActivity以讓用戶個性化應用程序的出現。 在此活動中,用戶可以選擇將應用程序保留在「燈光主題」(表示例如帶有黑色文本的白色背景)或「黑色主題」中,該燈光主題的相反顏色有利於夜晚使用。如何更新我的項目以創建多主題應用程序

這怎麼辦?

我正在考慮爲每個主題在xml中創建不同的佈局。

編輯

下面的圖片是SettingsActivity例子,我想改變appearence整個應用程序,而不是單一的活動。

enter image description here

enter image description here

enter image description here

enter image description here

回答

1

這就是我爲我的應用程序所做的。我相信我的方法可以提供幫助。

設置你的光明和黑暗的主題在styles.xml這樣的:

 <!-- Use this theme in Manifest by default --> 
    <style name="MyLightTheme" parent="Base.AppTheme.Light"></style> 
      
    <!-- Base light theme containing all styles --> 
    <style name="Base.AppTheme.Light" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     ... Other styles 
    </style> 

    <!-- Use this to switch to Dark theme --> 
    <style name="MyDarkTheme" parent="Base.AppTheme.Dark"></style> 

    <!-- Base dark theme containing all styles --> 
    <style name="Base.AppTheme.Dark" parent="Theme.AppCompat"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     ... Other styles 
    </style> 

既然你控制通過優先主題的變化,在你的PreferenceFragment註冊首選項更改偵聽。

PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this); 

實現onSharedPreferenceChanged()

@Override 
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 

     if (key.equals(getString(R.string.pref_key_nighttheme))) { 

      if (sharedPreferences.getBoolean(getString(R.string.pref_key_nighttheme), false)) { 
       // Night theme enabled 

       getActivity().setTheme(R.style.MyDarkTheme); 
         getActivity().getApplication().setTheme(R.style.MyDarkTheme); 
       darkTheme = true; 

      } else { 
       getActivity().setTheme(R.style.MyLightTheme); 
       getActivity().getApplication().setTheme(R.style.MyLightTheme); 
       darkTheme = false; 
      } 

      getActivity().recreate(); // This is important. It allows the theme change to take effect. 
     } 
    } 

一定要重新創建MainActivity的onResume()如果後退導航導致MainActivity。


此外,還必須爲您在每一個活動當前主題之前,超()被稱爲的onCreate()

isThemeDark = setDarkTheme(this); 

setDarkTheme()是我已經創建了一個輔助經由SharedPreference檢查當前主題。它檢查是否需要更改主題。

public static boolean setDarkTheme(Context context) { 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 

    boolean isDarkTheme = prefs.getBoolean(context.getString(R.string.pref_key_nighttheme), false); 
    context.setTheme(SettingsActivity.darkTheme ? R.style.MyDarkTheme : R.style.MyLightTheme); 

    return isDarkTheme; 
} 

這裏的夜間模式在我的應用程序Newslet是如何工作的:

enter image description here

UPDATE: Android現在正式通過其程序兼容性DayNight主題支持夜間模式。 這是一個tutorial在同一個。

2

您可以創建自己的主題,然後當用戶想改變他們的代碼添加到您的活動中,你可以選擇任何主題你不只是霍洛。

getApplication()。setTheme(Theme.Holo)

+0

看看,但我怎麼可能改變每佈局應用程序的簡單來自_SettingsActivity_中的_CheckBox_,全部採用單一樣式?我的意思是,我擁有的每個視圖都有其自己的「個性化」 – Pier

+0

,如果您不知道如何創建自己的樣式,您可以創建自己的樣式並從清單文件中更改每個活動的樣式,我會告訴您如何。 – RadiBarq

+0

也許我問的不清楚...我不想從清單中爲每個活動更改單個樣式。 我想要一個SettingsActivity,在其他個性化設置中,我可以在整個應用程序的多重主題(綠色,藍色,黑色,白色,紅色)之間切換,就像您可以在上面的編輯中看到的那樣。 每個佈局都有它自己的不同視圖,因此在我看來,難以在風格中進行所有這些更改 – Pier

2

也許這個回答可以幫助你解決這個問題 https://stackoverflow.com/a/4673209/4858673

public class BaseActivity extends Activity { 

    protected void onCreate(Bundle state) { 
     super.onCreate(state); 
     String theme = PreferenceManager.getDefaultSharedPreferences(this).getString("theme", "black"); 
     setTheme(getTheme(theme); 
    } 

    private int getTheme(String theme) { 
     if (theme.equals("black") return R.style.ThemeBlack; 
     if (theme.equals("white") return R.style.ThemeWhite; 
     ... 
     return android.R.style.Theme; // stub 
    } 
} 

而且所有的活動,從這個BaseActivity延伸。如果您想使用PreferenceActivity或PreferenceFragment,請將此代碼放入您的實現中,以便您只能擴展一個類 - Activity(AppCompatActivity)或PreferenceActivity

P.S. R.style.ThemeBlack是您在styles.xml中的主題設置。

<style name="ThemeBlack" parent="android:Theme.Holo"> 
</style> 
0
  1. 首先,您需要在xml樣式中創建一些明確定義的主題。

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> 
        <!-- Customize your theme here. --> 
        <item name="colorPrimary">@color/primaryColorAmber</item> 
        <item name="colorPrimaryDark">@color/primaryDarkColorAmber</item> 
        <item name="colorAccent">@color/secondaryColorAmber</item> 
    </style> 
    
    <style name="AppTheme.NoActionBar"> 
        <item name="windowActionBar">false</item> 
        <item name="windowNoTitle">true</item> 
    </style> 
    
    <style name="AppTheme.RED" parent="AppTheme.NoActionBar"> 
        <!-- Customize your theme here. --> 
        <item name="colorPrimary">@color/primaryColorRed</item> 
        <item name="colorPrimaryDark">@color/primaryDarkColorRed</item> 
        <item name="colorAccent">@color/secondaryColorRed</item> 
    </style> 
    
    <style name="AppTheme.PINK" parent="AppTheme.NoActionBar"> 
        <!-- Customize your theme here. --> 
        <item name="colorPrimary">@color/primaryColorPink</item> 
        <item name="colorPrimaryDark">@color/primaryDarkColorPink</item> 
        <item name="colorAccent">@color/secondaryColorPink</item> 
    </style> 
    
  2. 要在運行時使用下面您的基本活動的onCreate()方法只是之前的setContentView()的代碼,並更改主題。

    //若要更改主題,只需放入主題ID即可。

    int theme = getThemeFromPreferences(); // R.style.AppTheme_RED 
    setTheme(theme); 
    
  3. 要(從正在改變的主題),你需要通過調用下面這些活動的方法來重新創建活動的設置/偏好性的變化的主題。

    //保存您的主題ID優先

    saveThemeInPreferences(R.style.AppTheme_RED); 
    //recreate this activity to see changes 
    SettingActivity.this.recreate(); 
    

對於更多的細節和示例代碼...... 對 Android multi theme implementation

相關問題