不推荐使用Android context.getResources

不推荐使用Android context.getResources.updateConfiguration()

就在最近的context.getResources()。updateConfiguration()已在Android API 25中弃用,建议使用上下文。而是createConfigurationContext()

有谁知道createConfigurationContext如何用于覆盖android系统区域设置?

在此之前将通过以下方式完成:

Configuration config = getBaseContext().getResources().getConfiguration();config.setLocale(locale);context.getResources().updateConfiguration(config,
                                 context.getResources().getDisplayMetrics());


泛舟湖上清波郎朗
浏览 1600回答 3
3回答

12345678_0001

可能是这样的:Configuration overrideConfiguration = getBaseContext().getResources().getConfiguration();overrideConfiguration.setLocales(LocaleList);Context context  = createConfigurationContext(overrideConfiguration);Resources resources = context.getResources();奖励:使用createConfigurationContext()的博客文章

犯罪嫌疑人X

受书法和Mourjan以及我自己的启发,我创造了这个。首先,您必须创建Application的子类:public&nbsp;class&nbsp;MyApplication&nbsp;extends&nbsp;Application&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;Locale&nbsp;locale&nbsp;=&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;onCreate()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onCreate(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SharedPreferences&nbsp;preferences&nbsp;=&nbsp;PreferenceManager.getDefaultSharedPreferences(this); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Configuration&nbsp;config&nbsp;=&nbsp;getBaseContext().getResources().getConfiguration(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;lang&nbsp;=&nbsp;preferences.getString(getString(R.string.pref_locale),&nbsp;"en"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;systemLocale&nbsp;=&nbsp;getSystemLocale(config).getLanguage(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!"".equals(lang)&nbsp;&&&nbsp;!systemLocale.equals(lang))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;locale&nbsp;=&nbsp;new&nbsp;Locale(lang); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Locale.setDefault(locale); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setSystemLocale(config,&nbsp;locale); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;updateConfiguration(config); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;onConfigurationChanged(Configuration&nbsp;newConfig)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onConfigurationChanged(newConfig); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(locale&nbsp;!=&nbsp;null)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setSystemLocale(newConfig,&nbsp;locale); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Locale.setDefault(locale); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;updateConfiguration(newConfig); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;@SuppressWarnings("deprecation") &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;Locale&nbsp;getSystemLocale(Configuration&nbsp;config)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(Build.VERSION.SDK_INT&nbsp;>=&nbsp;Build.VERSION_CODES.N)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;config.getLocales().get(0); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;config.locale; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;@SuppressWarnings("deprecation") &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;void&nbsp;setSystemLocale(Configuration&nbsp;config,&nbsp;Locale&nbsp;locale)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(Build.VERSION.SDK_INT&nbsp;>=&nbsp;Build.VERSION_CODES.N)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config.setLocale(locale); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config.locale&nbsp;=&nbsp;locale; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;@SuppressWarnings("deprecation") &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;void&nbsp;updateConfiguration(Configuration&nbsp;config)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(Build.VERSION.SDK_INT&nbsp;>=&nbsp;Build.VERSION_CODES.JELLY_BEAN_MR1)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getBaseContext().createConfigurationContext(config); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getBaseContext().getResources().updateConfiguration(config,&nbsp;getBaseContext().getResources().getDisplayMetrics()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}}那么你需要将它设置为AndroidManifest.xml应用程序标签:<application &nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;android:name="path.to.your.package.MyApplication" &nbsp;&nbsp;&nbsp;&nbsp;>并将其添加到AndroidManifest.xml活动标记中。<activity &nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;android:configChanges="locale" &nbsp;&nbsp;&nbsp;&nbsp;>请注意pref_locale是一个字符串资源,如下所示:<string&nbsp;name="pref_locale">fa</string>如果未设置pref_locale,则硬编码“en”为默认值
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android