简体   繁体   中英

set new theme for app when switch fragment

I have various fragment. I need to change actionbar background color when switch fragment. I try to do it when select new view theme has change ,but still stay on current fragment. please help

oncreate use defualt theme

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this);
    setContentView(R.layout.activity_main);    

.

set new theme for fragment

public void displayView(int page) {
        // update the main content by replacing fragments
        fragment = null;
        switch (page) {
            case VIEW_HOME :
            fragment = new HomeFragment();
            break;
        case VIEW_BOTANICAL_DETAIL :
            fragment = new BotanicalFragment(); 
            hideActionBarMenu(); 
            break;
        case VIEW_MAP :
            fragment = new MapFragment();
            break;
        case VIEW_ABOUT :
            hideActionBarMenu(R.id.actionbar_search);
            fragment = new AboutFragment();
            break;
        case VIEW_AR:
            hideActionBarMenu(R.id.actionbar_search);
            fragment = new ARFragment();
            break;
            case VIEW_ALL:
                Utils.changeToTheme(this, Utils.THEME_DEFAULT);
                fragment = new HomeFragment();
                break;
            case VIEW_RAIN:
                Utils.changeToTheme(this, Utils.THEME_GREEN);
                fragment = new RainFragment();
                break;

            default:
                fragment = new MainFragment();
                break;
        }

.

public class Utils { 
    private static int sTheme; 
    public final static int THEME_DEFAULT = 0; 
    public final static int THEME_GREEN = 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.AppTheme); 
            break; 
            case 
            THEME_GREEN: activity.setTheme(R.style.Actionbar2); 
            break; 
             } } }

您可以调用onCreateView: getApplication().setTheme(Theme.Holo);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM