Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 切换片段时为应用程序设置新主题_Java_Android_Mobile_Themes - Fatal编程技术网

Java 切换片段时为应用程序设置新主题

Java 切换片段时为应用程序设置新主题,java,android,mobile,themes,Java,Android,Mobile,Themes,我有各种各样的碎片。切换片段时,我需要更改actionbar背景色。当“选择新视图”主题发生更改时,我会尝试这样做,但仍然停留在当前片段上。请帮忙 oncreate使用defualt主题 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Utils.onActivityCreateSetTheme(this); setContentView(R.

我有各种各样的碎片。切换片段时,我需要更改actionbar背景色。当“选择新视图”主题发生更改时,我会尝试这样做,但仍然停留在当前片段上。请帮忙

oncreate使用defualt主题

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

为片段设置新主题

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;
        }


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

如果您为自己的主题使用appcompat或类似版本,请注意:
getApplication().setTheme(R.layout.AppTheme)
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; 
             } } }