首先,你应该知道在25个
API Resources.updateConfiguration(...)中已弃用。因此,你可以执行以下操作:
1)你需要创建自己的
ContextWrapper,它将覆盖baseContext中的所有配置参数。例如,这是我的ContextWrapper,可以正确更改Locale。注意
context.createConfigurationContext(configuration)方法。
public class ContextWrapper extends android.content.ContextWrapper { public ContextWrapper(Context base) { super(base); } public static ContextWrapper wrap(Context context, Locale newLocale) { Resources res = context.getResources(); Configuration configuration = res.getConfiguration(); if (BuildUtils.isAtLeast24Api()) { configuration.setLocale(newLocale); LocaleList localeList = new LocaleList(newLocale); LocaleList.setDefault(localeList); configuration.setLocales(localeList); context = context.createConfigurationContext(configuration); } else if (BuildUtils.isAtLeast17Api()) { configuration.setLocale(newLocale); context = context.createConfigurationContext(configuration); } else { configuration.locale = newLocale; res.updateConfiguration(configuration, res.getDisplayMetrics()); } return new ContextWrapper(context); }}2)这是你在
baseActivity中应该做的事情:
@Overrideprotected void attachbaseContext(Context newbase) { Locale newLocale; // .. create or get your new Locale object here. Context context = ContextWrapper.wrap(newbase, newLocale); super.attachbaseContext(context);}注意:
如果要在某个地方更改应用程序的区域设置,请记住要重新创建活动。你可以使用此解决方案覆盖所需的任何配置。



