2016-05-17 3 views
1

Ich versuche, eine Anwendung mit 2 Thema zu machen, kann ich dies nur für eine Aktivität tun und andere Aktivitäten sind nicht mehr betroffen, Wie ändere ich das Thema für alle Seiten gelten?mit 2 Thema für Android-App

Codes für Themen:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">#795548</item> 
    <item name="colorPrimaryDark">#5d4037</item> 
    <item name="colorAccent">#e6300b</item> 
    <item name="android:textColorPrimary">#ffffff</item> 
    <item name="android:navigationBarColor">#e4ccc7</item> 


</style> 


<style name="apptheme2" parent="Theme.AppCompat.Light.NoActionBar"> 

    <item name="colorPrimary">#8ff2f2</item> 
    <item name="colorPrimaryDark">#044949</item> 
    <item name="colorAccent">#813ba4</item> 
    <item name="android:textColorPrimary">#000000</item> 
    <item name="android:navigationBarColor">#735852</item> 

Java-Codes für eine Aktivität:

Button btndark; 
Button btnlight; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 

    final SharedPreferences shared = getSharedPreferences("prefers", 0); 
    final Boolean theme = shared.getBoolean("theme", true); 

    if (!theme) { 
     setTheme(R.style.AppTheme); 
    } else { 

     setTheme(R.style.apptheme2); 
    } 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    btndark = (Button) findViewById(R.id.btn1); 
    btnlight = (Button) findViewById(R.id.btn2); 


    btndark.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      SharedPreferences.Editor editor = shared.edit(); 
      editor.putBoolean("theme", false); 
      editor.commit(); 
      recreate(); 
     } 
    }); 


    btnlight.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      SharedPreferences.Editor editor = shared.edit(); 
      editor.putBoolean("theme", true); 
      editor.commit(); 
      recreate(); 

     } 
    }); 
+0

Verwenden Sie das Manifest zum Festlegen von Themen –

Antwort

1

diesen Code versuchen: schreiben Sie diesen Code in Ihrem Manifest

<activity 
     android:name="Activity1" 
     android:theme="@style/AppTheme1" /> 

für eine andere Tätigkeit

<activity 
     android:name="Activity2" 
     android:theme="@style/AppTheme2" /> 
0

Erweitert die Aktivität in Ihrer Aktivität. Sie können Integer in den gemeinsamen Einstellungen speichern. So können Sie wissen, welches Thema ausgewählt ist.

public class BaseActivity extends Activity { 

public static int themePrimaryColor = R.color.ColorPrimary; 
public static int themePrimaryColorDark = R.color.ColorPrimaryDark; 
public static int themeAccentColor = R.color.colorAccent; 

//for theme color 
public static final int THEME_LIGHT = 1; 
public static final int THEME_DARK = 2; 

public static int currentTheme = R.style.AppTheme; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
int themeColor = SettingPreference.getIntValue(SettingPreference.KEY_APP_THEME, THEME_LIGHT, this); 
    selectTheme(themeColor); 
    super.setTheme(currentTheme); 
super.onCreate(savedInstanceState); 
} 

public static void selectTheme(int themeColor){ 
    switch (themeColor) { 
     case THEME_LIGHT: 
      setCurrentTheme(R.style.AppTheme); 
      themePrimaryColor = R.color.ColorPrimary; 
      themePrimaryColorDark = R.color.ColorPrimaryDark; 
      themeAccentColor = R.color.colorAccent; 

      break; 

     case THEME_DARK: 
      setCurrentTheme(R.style.AppTheme2); 
      themePrimaryColor = R.color.color_primary2; 
      themePrimaryColorDark = R.color.color_primary_dark2; 
      themeAccentColor = R.color.color_accent2; 
      break; 


     default: 
      setCurrentTheme(R.style.AppTheme); 
      themePrimaryColor = R.color.ColorPrimary; 
      themePrimaryColorDark = R.color.ColorPrimaryDark; 
      themeAccentColor = R.color.colorAccent; 
    } 
} 

public static int getCurrentTheme() { 
    return currentTheme; 
} 

public static void setCurrentTheme(int currentTheme) { 
    BaseAppCompatActivity.currentTheme = currentTheme; 
} 

} 

Nach dem Neustart Ihrer Anwendung wird Ihr Anwendungsthema entsprechend Ihrer Auswahl geändert.