2014-02-25 38 views
1

我正在創建一個應用程序,我想要更改完整應用程序的主題。即如果我點擊任何Button說changeTheme它應該改變主題w.r.t ApplicationContext更改主題的完整應用程序

我試圖

OnClickListener onClick = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

       getApplicationContext().setTheme(R.style.AppTheme1); 
       setContentView(R.layout.activity_main); 


     } 
    }; 

但這不是反映。是否有我需要把這樣我可以改變ApplicationThemes

添加styles.xml

<style name="AppBaseTheme" parent="android:Theme.Light"> 
     <!-- 
      Theme customizations available in newer API levels can go in 
      res/values-vXX/styles.xml, while customizations related to 
      backward-compatibility can go here. 
     --> 
    </style> 

    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
     <item name="android:textColor">#ffffff</item> 
    </style> 

    <style name="AppTheme1" parent="AppBaseTheme"> 
     <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
     <item name="android:textColor">#000000</item> 
    </style> 
+0

應用主題,體現在應用標籤 – Raghunandan

+0

@ Raghunandan我在清單文件中應用了主題。但我需要更改主題運行時。 – Vaibs

回答

0

java的如下

Utils.java

import android.app.Activity; 
import android.content.Intent; 
public class Utils 
{ 
    private static int sTheme; 
    public final static int THEME_DEFAULT = 0; 
    public final static int THEME_WHITE = 1; 
    public final static int THEME_BLUE = 2; 
    /** 
     * Set the theme of the Activity, and restart it by creating a new Activity of the same type. 
     */ 
    public static void changeToTheme(Activity activity, int theme) 
    { 
      sTheme = theme; 
      activity.finish(); 
activity.startActivity(new Intent(activity, activity.getClass())); 
    } 
    /** Set the theme of the activity, according to the configuration. */ 
    public static void onActivityCreateSetTheme(Activity activity) 
    { 
      switch (sTheme) 
      { 
      default: 
      case THEME_DEFAULT: 
       activity.setTheme(R.style.FirstTheme); 
       break; 
      case THEME_WHITE: 
       activity.setTheme(R.style.SecondTheme); 
       break; 
      case THEME_BLUE: 
       activity.setTheme(R.style.Thirdheme); 
       break; 
      } 
    } 
} 

和任何修改使用方法如下

Utils.changeToTheme(this, Utils.THEME_WHITE); 

您可以按如下

@Override 
    public void onClick(View v) 
    { 
      // TODO Auto-generated method stub 
      switch (v.getId()) 
      { 
      case R.id.button1: 
      Utils.changeToTheme(this, Utils.THEME_DEFAULT); 
      break; 
      case R.id.button2: 
      Utils.changeToTheme(this, Utils.THEME_WHITE); 
      break; 
      case R.id.button3: 
      Utils.changeToTheme(this, Utils.THEME_BLUE); 
      break; 
      } 
    } 

寫一個點擊事件,你可以有主題風格如下

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="FirstTheme" > 
     <item name="android:textColor">#FF0000</item> 
    </style> 
    <style name="SecondTheme" > 
     <item name="android:textColor">#00FF00</item> 
    </style> 
    <style name="Thirdheme" > 
     <item name="android:textColor">#0000F0</item> 
    </style> 
</resources> 

對於完整的示例,請訪問http://mrbool.com/how-to-change-the-layout-theme-of-an-android-application/25837

+0

這將適用於特定的活動,而不適用於完整的應用程序。 – Vaibs

+0

是的佈局,所以你可以設置編程! –

+0

什麼可以是另一種解決方案是以同樣的方式嘗試,並從你的項目的應用程序類來做! –

相關問題