我有各种片段。我需要在切换片段时更改操作栏背景颜色。我选择在新的视图主题发生变化时尝试这样做,但仍保留在当前片段上。请帮忙
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;
}
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;
} } }
答案 0 :(得分:0)
您可以调用onCreateView:getApplication().setTheme(Theme.Holo);