2016-02-15 126 views
3

我有兩個活動。一個是主要活動,另一個是設置活動。如何從其他活動設置主題到主要活動?

當用戶從顏色選擇器對話框中選擇顏色時,我想從設置活動中更改主題。

現在我可以改變主題,但它只是改變主題活動的設置活動的主題。我如何將主題設置爲主要活動?

設置活動:

public class Settings extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 


      Theme.onActivityCreateSetTheme(this); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_settings); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 


    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 



    final ColorPickerDialog colorPickerDialog = new ColorPickerDialog(); 
    colorPickerDialog.initialize(R.string.dialog_title, new int[]{Color.CYAN, Color.LTGRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED, Color.GRAY, Color.YELLOW}, Color.YELLOW, 3, 2); 
    colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() { 

     @Override 
     public void onColorSelected(int color) { 



      // startActivityForResult(new Intent(getApplicationContext(), 
      //   ThemedPreferenceActivity.class), SETTINGS_ACTION); 

      if (color == Color.CYAN) { 
       Theme.changeToTheme(Settings.this, Theme.THEME_DEFAULT); 
       no = 0; 
      } 
      else if (color == Color.LTGRAY) 

      { 
       Theme.changeToTheme(Settings.this, Theme.THEME_WHITE); 
       no = 1; 
      } 
      else if (color == Color.BLACK) { 

       Theme.changeToTheme(Settings.this,Theme.THEME_BLUE); 
       no = 3; 

      } 
      Toast.makeText(Settings.this, "selectedColor : " + color, Toast.LENGTH_SHORT).show(); 
     } 

    }); 



    SharedPreferences.Editor editor = sharedpreferences.edit(); 

    editor.putInt("color_1",no); 
    editor.putInt("color_2",no); 
    editor.putInt("color_3",no); 
    editor.commit(); 

    LinearLayout theme = (LinearLayout)findViewById(R.id.theme); 

    theme.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      colorPickerDialog.show(getSupportFragmentManager(), "colorpicker"); 
     } 
    }); 


} 

主題:

public class Theme { 

    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.BlueTheme); 
       break; 
     } 
    } 
} 

編輯:

主要活動:

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
    SharedPreferences.Editor editor = pref.edit(); 


    no = pref.getInt("color_1", 0);   
    no = pref.getInt("color_2", 0); 
    no = pref.getInt("color_3", 0); 

    if(no == 0) 
    { 
     setTheme(R.style.AppTheme); 
    } 

    else if(no == 1) 
    { 
     setTheme(R.style.AppTheme_Solarized); 
    } 

    else if(no == 2) 
    { 
     setTheme(R.style.BlueTheme); 
    } 


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

謝謝。

+0

看到它可能會有所幫助的問題使用你可以改變顏色有些變色的觀點。 [如何在運行時更改當前主題在Android](http://stackoverflow.com/questions/2482848/how-to-change-current-theme-at-runtime-in-android) –

回答

0
 public void updateTheme(int themeID) { 
     setTheme(themeID); 
     Intent intent = getIntent(); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
     finish(); 
     overridePendingTransition(0, 0); 
     startActivity(intent); 
     overridePendingTransition(0, 0); 
    } 
+0

我想動態地更改主題時用戶從顏色選擇器對話框中選擇顏色。這已經在manifest中設置。@ ahmad aghazadeh – user5881997

+0

如何以及在何處使用此方法? – user5881997

0

除非重新啓動當前活動,否則不能動態更改主題。設置內容視圖檢查共享偏好從數據,並據此設置主題之前

  1. 活動的店鋪的onCreate:

    你可以做到這一點使用兩種方法爲您的問題。 從設置活動設置共享數據。 在MainActivity檢查共享數據。

    public void onCreate(Bundle savedInstanceState) 
    { 
        if (checkColorData("navcolor").equals("SomeColor")) 
        { 
         setTheme(R.style.AppTheme); 
        } 
        else 
        { 
         setTheme(R.style.AppThemeDarkNav); 
        } 
    } 
    
  2. 的動作欄使用工具欄,並通過調用setBackgoundColor

+0

我試過這個,主要活動仍然沒有變化。請你可以檢查編輯過的問題,告訴我什麼是錯的?@Flygenring,@Bharath Kumar – user5881997

相關問題