我一直致力于在当前应用程序中实现主题转换器。
我有一个首选项屏幕设置,允许用户在两个主题之间进行选择,黑暗和浅色。
我的活动是这样设置的;
现在我遇到了活动A的问题。似乎是将更改的主题仅应用于ActionBar上的Action overflow(下拉框)(下面的截图)
我在活动的onCreate方法中设置了该组。
正如您所看到的,只有溢出应用了灯光主题。
这是我的ThemeUtils类。
public class ThemeUtils
{
public final static int THEME_DEFAULT = 0;
public final static int THEME_LIGHT = 1;
public final static int THEME_DARK = 2;
private static int mTheme;
/**
* Sets the theme of the activity and restarts it by creating a new Activity of the same type.
* @param activity Activity that initiates the theme change
* @param theme An int value to identify which theme to change to - (use ThemeUtils constants)
*/
public static void changeToTheme( final Activity activity, final int theme )
{
mTheme = theme;
finishAndRestartActivity( activity );
}
/**
* Sets the theme of the activity, according to the value of mTheme
* @param activity The activity to set the theme to
*/
public static void setTheme( final Activity activity )
{
switch ( mTheme )
{
case THEME_LIGHT:
activity.setTheme( R.style.Result_Theme_Light );
break;
case THEME_DARK:
activity.setTheme( R.style.Result_Theme_Dark );
break;
default:
case THEME_DEFAULT:
activity.setTheme( R.style.Result_Theme_Dark );
break;
}
}
public static int getTheme()
{
return mTheme;
}
public static boolean canSetThemeFromPrefs( final Activity activity )
{
boolean result = false;
SharedPreferences prefMngr = PreferenceManager.getDefaultSharedPreferences( activity );
if ( prefMngr.contains( "theme_pref" ) )
{
result = true;
}
return result;
}
public static int getThemeFromPrefs( final Activity activity )
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences( activity );
final String themeFromPrefs = preferences.getString( "theme_pref", "theme_default" );
if ( themeFromPrefs.equals( "theme_light" ) )
{
mTheme = THEME_LIGHT;
}
else if ( themeFromPrefs.equals( "theme_dark" ) )
{
mTheme = THEME_DARK;
}
else
{
mTheme = THEME_DEFAULT;
}
return getTheme();
}
public static int getThemeFromPrefs( final String key )
{
if ( key.equals( "theme_light" ) )
{
mTheme = THEME_LIGHT;
}
else if ( key.equals( "theme_dark" ) )
{
mTheme = THEME_DARK;
}
else
{
mTheme = THEME_DEFAULT;
}
return getTheme();
}
private static void finishAndRestartActivity( final Activity activity )
{
activity.finish();
activity.startActivity( new Intent( activity, activity.getClass() ) );
}
}
任何提示/想法?
谢谢, 萨姆。