从片段更改活动主题 [英] Change Activity Theme From a Fragment

查看:61
本文介绍了从片段更改活动主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置PreferenceFragment,该设置允许用户选择主题.用户可以选择深色或浅色主题.选择主题后,用户可以按后退"按钮返回上一个片段.这称为包含活动的onCreate方法,在其中读取和应用主题.但是,该主题无法正确应用,从Holo.Light切换到Holo.Dark会更改背景色,操作栏等,但不会更改文本,从而导致文本褪色且难以阅读.有什么想法我做错了吗?我读过的所有内容都说该主题应该应用在onCreate方法中,这就是我正在做的事情.

I have a settings PreferenceFragment that allows the user to select a theme. The user can select a dark or light theme. After selecting a theme the user presses the back button to return to the previous fragment. This called the containing activity's onCreate method where the theme is read and applied. However the theme is not applied correctly, Switching from Holo.Light to Holo.Dark changes the background colour, action bar etc but does not change the text resulting in faded, hard to read text. Any ideas what I am doing wrong? Everything I have read says that the theme should be applied in the onCreate method and that is what I am doing.

预先感谢您的帮助.

编辑

此处要求的是相关代码.

As requested here is the relevant code.

public class MainActivity extends Activity {

     private ActionBarDrawerToggle mSlideMenuToggle;
     private boolean isDarkTheme;
     private static final String InitializedKey = "initialized";

     @Override
 protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
          applySettings();
          setContentView(R.layout.activity_main);
       }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
          if (mSlideMenuToggle.onOptionsItemSelected(item)) {
               return true;
          } else if (item.getItemId() == R.id.menu_settings) {
               getFragmentManager().beginTransaction()
                        .replace(R.id.content_frame, new SettingsFragment())
                        .addToBackStack(null)
                        .commit();
               return true;
          } else {
               return super.onOptionsItemSelected(item);
          }
     }

     private void applySettings() {
          isDarkTheme = PreferenceManager.getDefaultSharedPreferences(this).getString(SettingsFragment.ThemeSetting, null).equals("1");
          if (isDarkTheme) {
               setTheme(android.R.style.Theme_Holo);
          } else {
               setTheme(android.R.style.Theme_Holo_Light);
     }
}

onCreate方法通过调用applySettings将当前主题应用于活动.选项菜单允许创建SettingsFragment.

The onCreate method applies the current theme to the activity by calling applySettings. The options menu allows for a SettingsFragment to be created.

public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

   public static final String ThemeSetting = "isDarkTheme";

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       addPreferencesFromResource(R.xml.preferences);
   }

   @Override
   public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
       if (key.equals(ThemeSetting)) {
           String[] themes = getResources().getStringArray(R.array.isDarkThemeStrings);
           findPreference(key).setSummary(sharedPreferences.getString(key, "").equals("0") ? themes[0] : themes[1]);
       }
   }

   @Override
   public void onResume() {
       super.onResume();
       PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);
   }

   @Override
   public void onPause() {
       super.onPause();
       PreferenceManager.getDefaultSharedPreferences(getActivity()).unregisterOnSharedPreferenceChangeListener(this);
   }

 }

SettingsFragment具有一个设置,主题,可以具有两个值之一,即暗"或亮".用户选择一个,然后单击后退"按钮.这将导致调用MainActivityonCreate方法,再次应用设置,但方法不正确.

The SettingsFragment has one setting, Theme which can have one of two values, Dark or Light. The user picks one and then hits the back button. This causes the onCreate method of the MainActivity to be called, again applying the settings but not correctly.

有人吗?我感觉一旦在SettingsFragment中更改了主题,然后按了后退按钮,则该主题应应用于MainActivity,但不是.大多数元素都会更改,但文本会保持深色(从Holo.Light变为Holo.Dark).

Anyone? I feel like that once the theme is changed in the SettingsFragment and then the back button is pressed the theme should be applied to MainActivity but it is not. Most elements change but the text stays dark (going from Holo.Light to Holo.Dark).

推荐答案

我想可以为您提供帮助.在过去的几个月中,我花了很多时间来为我的应用程序做同样的事情.

I think I can help you with this. I spent a lot of time over the last few months working on the exact same thing for my app.

以上海报并不完全正确.在实例化视图之前-在setContentView()之前,您需要在onCreate()中设置主题.何时调用super.onCreate()并不重要.我在上面的代码中没有看到setContentView(),所以我想知道是否删除了它?

The above poster isn't exactly correct. You need to set the theme in onCreate() before views are instantiated -- before setContentView(). When super.onCreate() is called isn't important. I don't see setContentView() in your code above, so I'm wondering if you removed it?

但是,如果旋转时您的活动主题正确(因为在方向改变时销毁并重新创建了活动),那么主题的设置就没有错.相反,我倾向于认为您在退出SettingsFragment时误认为onCreate()被调用.

However, if your activity is being themed correctly when you rotate (because it is destroyed and recreated on orientation change) then there's nothing wrong with how you're setting the theme. Instead, I'm inclined to think you're mistaken about onCreate() being called when you exit the SettingsFragment.

您可以像这样强制您的活动重新创建自己:

You can force your activity to recreate itself like this:

finish();
startActivity(getIntent());

请首先尝试在活动的onCreate()方法中设置一个断点,并确认退出片段时是否击中了断点(我敢打赌不是.)然后尝试上面的代码.

Please first try setting a breakpoint in your activity's onCreate() method and confirm whether it's hit on exiting your fragment (I bet it's not.) Then try the code above.

这篇关于从片段更改活动主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆