2017-01-23 10 views
3

У меня есть один экран настроек для темы приложения изменений во время выполнения. Я знаю, как создать тему материального дизайна. У меня есть создать в моем style.xml файлеRuntime Change Тема для Android темы

Вот код моего style.xml:

<style name="AppTheme" parent="AppTheme.Base"/> 

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <item name="colorPrimary">@color/primaryBackground</item> 
     <item name="colorPrimaryDark">@color/primaryBackground</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:colorControlNormal">@color/primaryBackground</item> 
     <item name="android:colorControlActivated">@color/primaryBackground</item> 
     <item name="android:colorControlHighlight">@color/primaryBackground</item> 
     <item name="android:textColorPrimary">@color/primaryBackground</item> 
     <item name="android:textColorSecondary">@color/primaryBackground</item> 
     <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item> 
     <item name="android:textCursorDrawable">@drawable/cursor_indicator</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

Теперь я хочу изменить во время выполнения приложения темы от зеленого до фиолетового или желтого цвета. Что-нибудь может сказать мне, как я могу создать подборщик цветов из выбора темы и как я могу создать несколько тем в моем style.xml, чтобы изменить его на время выполнения.

+1

По времени выполнения , вы имеете в виду, когда приложение действительно работает? – jitinsharma

+0

yes уже настроенная зеленая тема теперь пользователь может выбрать любой другой цвет с экрана настройки, тогда вся тема приложения должна быть изменена. –

ответ

3

Вы видели эту демо?

MultipleThemeMaterialDesign

Смотреть это :

public class Preferences { 

    private static final BoolToStringPref[] PREF_MIGRATION = new BoolToStringPref[]{ 
      new BoolToStringPref(R.string.pref_dark_theme, false, 
        R.string.pref_theme, R.string.pref_theme_value_red), 
    }; 

    public static void sync(PreferenceManager preferenceManager) { 
     Map<String, ?> map = preferenceManager.getSharedPreferences().getAll(); 
     for (String key : map.keySet()) { 
      sync(preferenceManager, key); 
     } 
    } 

    public static void sync(PreferenceManager preferenceManager, String key) { 
     Preference pref = preferenceManager.findPreference(key); 
     if (pref instanceof ListPreference) { 
      ListPreference listPref = (ListPreference) pref; 
      pref.setSummary(listPref.getEntry()); 
     } 
    } 

    /** 
    * Migrate from boolean preferences to string preferences. Should be called only once 
    * when application is relaunched. 
    * If boolean preference has been set before, and value is not default, migrate to the new 
    * corresponding string value 
    * If boolean preference has been set before, but value is default, simply remove it 
    * @param context application context 
    * TODO remove once all users migrated 
    */ 
    public static void migrate(Context context) { 
     SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
     SharedPreferences.Editor editor = sp.edit(); 
     for (BoolToStringPref pref : PREF_MIGRATION) { 
      if (pref.isChanged(context, sp)) { 
       editor.putString(context.getString(pref.newKey), context.getString(pref.newValue)); 
      } 

      if (pref.hasOldValue(context, sp)) { 
       editor.remove(context.getString(pref.oldKey)); 
      } 
     } 

     editor.apply(); 
    } 

    public static void applyTheme(ContextThemeWrapper contextThemeWrapper) { 
     if (Preferences.darkThemeEnabled(contextThemeWrapper)) { 
      contextThemeWrapper.setTheme(R.style.AppTheme_Blue); 
     } 
    } 

    private static boolean darkThemeEnabled(Context context) { 
     return PreferenceManager.getDefaultSharedPreferences(context) 
       .getString(context.getString(R.string.pref_theme), 
         context.getString(R.string.pref_theme_value_red)) 
       .equals(context.getString(R.string.pref_theme_value_blue)); 
    } 

    private static class BoolToStringPref { 
     private final int oldKey; 
     private final boolean oldDefault; 
     private final int newKey; 
     private final int newValue; 

     private BoolToStringPref(@StringRes int oldKey, boolean oldDefault, 
           @StringRes int newKey, @StringRes int newValue) { 
      this.oldKey = oldKey; 
      this.oldDefault = oldDefault; 
      this.newKey = newKey; 
      this.newValue = newValue; 
     } 

     private boolean isChanged(Context context, SharedPreferences sp) { 
      return hasOldValue(context, sp) && 
        sp.getBoolean(context.getString(oldKey), oldDefault) != oldDefault; 
     } 

     private boolean hasOldValue(Context context, SharedPreferences sp) { 
      return sp.contains(context.getString(oldKey)); 
     } 
    } 
} 

Checkout, что демо, он будет поможет вам понять больше.

+0

удивительный образец работал как шарм +1 для этого –

1

Динамические темы с использованием style.xml

Вот мой код

Style.xml

<resources> 

    <style name="AppTheme.Base.Green" parent="AppTheme.Green"> 
     <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
     <item name="windowActionModeOverlay">true</item> 
     <item name="windowActionBarOverlay">true</item> 
     <item name="android:windowActionBarOverlay">true</item> 
     <item name="android:windowBackground">@color/activity_bg</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <style name="AppTheme.Base.Green.Dark" parent="AppTheme.Green"> 
     <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
     <item name="windowActionModeOverlay">true</item> 
     <item name="windowActionBarOverlay">true</item> 
     <item name="android:windowActionBarOverlay">true</item> 
     <item name="android:windowBackground">@color/activity_bg_black</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <style name="AppTheme.Base.Purple" parent="AppTheme.Purple"> 
     <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
     <item name="windowActionModeOverlay">true</item> 
     <item name="windowActionBarOverlay">true</item> 
     <item name="android:windowActionBarOverlay">true</item> 
     <item name="android:windowBackground">@color/activity_bg</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <style name="AppTheme.Base.Purple.Dark" parent="AppTheme.Purple"> 
     <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
     <item name="windowActionModeOverlay">true</item> 
     <item name="windowActionBarOverlay">true</item> 
     <item name="android:windowActionBarOverlay">true</item> 
     <item name="android:windowBackground">@color/activity_bg_black</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <!-- Base application themes. --> 
    <style name="ThemeApp.Green" parent="AppTheme.Base.Green"/> 
    <style name="ThemeApp.Green.Dark" parent="AppTheme.Base.Green.Dark"/> 
    <style name="ThemeApp.Purple" parent="AppTheme.Base.Purple"/> 
    <style name="ThemeApp.Purple.Dark" parent="AppTheme.Base.Purple.Dark"/> 


    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> 
     <item name="spinBars">true</item> 
     <item name="color">@android:color/white</item> 
    </style> 


</resources> 

themes.xml

v21/style.xml

<resources> 

    <style name="AppTheme.Base.Green" parent="AppTheme.Green"> 
     <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
     <item name="windowActionModeOverlay">true</item> 
     <item name="windowActionBarOverlay">true</item> 
     <item name="android:windowActionBarOverlay">true</item> 
     <item name="android:windowBackground">@color/activity_bg</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
     <item name="android:navigationBarColor">@color/primary_green</item> 
    </style> 

    <style name="AppTheme.Base.Green.Dark" parent="AppTheme.Green"> 
     <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
     <item name="windowActionModeOverlay">true</item> 
     <item name="windowActionBarOverlay">true</item> 
     <item name="android:windowActionBarOverlay">true</item> 
     <item name="android:windowBackground">@color/activity_bg_black</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
     <item name="android:navigationBarColor">@color/primary_green</item> 
    </style> 

    <style name="AppTheme.Base.Purple" parent="AppTheme.Purple"> 
     <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
     <item name="windowActionModeOverlay">true</item> 
     <item name="windowActionBarOverlay">true</item> 
     <item name="android:windowActionBarOverlay">true</item> 
     <item name="android:windowBackground">@color/activity_bg</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
     <item name="android:navigationBarColor">@color/primary_purple</item> 
    </style> 

    <style name="AppTheme.Base.Purple.Dark" parent="AppTheme.Purple"> 
     <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
     <item name="windowActionModeOverlay">true</item> 
     <item name="windowActionBarOverlay">true</item> 
     <item name="android:windowActionBarOverlay">true</item> 
     <item name="android:windowBackground">@color/activity_bg_black</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
     <item name="android:navigationBarColor">@color/primary_purple</item> 
    </style> 

    <!-- Base application themes. --> 
    <style name="ThemeApp.Green" parent="AppTheme.Base.Green"/> 
    <style name="ThemeApp.Green.Dark" parent="AppTheme.Base.Green.Dark"/> 
    <style name="ThemeApp.Purple" parent="AppTheme.Base.Purple"/> 
    <style name="ThemeApp.Purple.Dark" parent="AppTheme.Base.Purple.Dark"/> 


    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> 
     <item name="spinBars">true</item> 
     <item name="color">@android:color/white</item> 
    </style> 


    <!-- <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

     <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />--> 

</resources> 

метод для изменения темы выполнения

private void setAppTheme() { 

     if (!MainController.preferenceGetString(Theme_Current, "").equals("")) { 
      if (MainController.preferenceGetString(Theme_Current, "").equals("Green")) { 
       setTheme(R.style.ThemeApp_Green); 
      } else if (MainController.preferenceGetString(Theme_Current, "").equals("Green_Dark")) { 
       setTheme(R.style.ThemeApp_Green_Dark); 
      } else if (MainController.preferenceGetString(Theme_Current, "").equals("Purple_Dark")) { 
       setTheme(R.style.ThemeApp_Purple_Dark); 
      } else if (MainController.preferenceGetString(Theme_Current, "").equals("Purple")) { 
       setTheme(R.style.ThemeApp_Purple); 
      } 
     } else { 
      setTheme(R.style.ThemeApp_Green); 
     } 
    } 

после выполнения этого метода перезагрузите деятельность необходимо

полный исходный код https://github.com/rkoshti/DynamicMaterialTheme

 Смежные вопросы

  • Нет связанных вопросов^_^