![](/img/trans.png)
[英]What is the best way to update android widget on screen_on. Is it supported in Android Oreo (API 26)
[英]How to change Android O / Oreo / api 26 app language
我想改变应用程序的语言,直到API 26才能正常工作。
对于api> 25,我放了Locale.setDefault(Locale.Category.DISPLAY, mynewlanglocale);
在setContentView(R.layout.activity_main);
之前setContentView(R.layout.activity_main);
但没有变化。
文档没有解释太多。
是的在android Oreo本地化与updateconfiguration无法正常工作。 但它在Android N本身被弃用了。 而不是更新配置使用每个attachcontext中的createconfiguration。 它对我来说很好。 试试这个...
在你的活动中添加这个..
@Override
protected void attachBaseContext(Context newBase) {
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
super.attachBaseContext(MyContextWrapper.wrap(newBase, "ta"));
}
else {
super.attachBaseContext(newBase);
}
}
在MyContextWrapper.java中
public static ContextWrapper wrap(Context context, String language) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
Locale newLocale = new Locale(language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
} else {
configuration.locale = newLocale;
res.updateConfiguration(configuration, res.getDisplayMetrics());
}
return new ContextWrapper(context);
}
我有同样的问题:因为Android 8.0+我的应用程序的某些部分不再改变他们的语言。 更新应用程序和活动上下文对我有帮助。 以下是MainActivity功能的示例:
private void setApplicationLanguage(String newLanguage) {
Resources activityRes = getResources();
Configuration activityConf = activityRes.getConfiguration();
Locale newLocale = new Locale(newLanguage);
activityConf.setLocale(newLocale);
activityRes.updateConfiguration(activityConf, activityRes.getDisplayMetrics());
Resources applicationRes = getApplicationContext().getResources();
Configuration applicationConf = applicationRes.getConfiguration();
applicationConf.setLocale(newLocale);
applicationRes.updateConfiguration(applicationConf,
applicationRes.getDisplayMetrics());
}
不推荐使用updateConfiguration
,您应该使用createConfigurationContext
。 我这样解决了:
@Override
protected void attachBaseContext(Context newBase) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Configuration config = newBase.getResources().getConfiguration();
//Update your config with the Locale i. e. saved in SharedPreferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(newBase);
String language = prefs.getString(SP_KEY_LANGUAGE, "en_US");
Locale.setDefault(locale);
config.setLocale(new Locale(language));
newBase = newBase.createConfigurationContext(config);
}
super.attachBaseContext(newBase);
}
更新了所有Android版本,直到奥利奥
创建一个这样的类
public class LocaleUtils {
@Retention(RetentionPolicy.SOURCE)
@StringDef({ENGLISH, FRENCH, SPANISH})
public @interface LocaleDef {
String[] SUPPORTED_LOCALES = {ENGLISH, FRENCH, SPANISH};
}
public static final String ENGLISH = "en";
public static final String FRENCH = "fr";
public static final String SPANISH = "es";
public static void initialize(Context context) {
setLocale(context, ENGLISH);
}
public static void initialize(Context context, @LocaleDef String defaultLanguage) {
setLocale(context, defaultLanguage);
}
public static boolean setLocale(Context context, @LocaleDef String language) {
return updateResources(context, language);
}
private static boolean updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
context.createConfigurationContext(configuration);
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return true;
}
}
现在,当您从应用中选择语言时,请将语言代码保存在共享首选项中,如下所示
private static SharedPreferences getDefaultSharedPreference(Context context) {
if (PreferenceManager.getDefaultSharedPreferences(Application.getInstance().getApplicationContext()) != null)
return PreferenceManager.getDefaultSharedPreferences(Application.getInstance().getApplicationContext());
else
return null;
}
public static void setSelectedLanguageId(String id){
final SharedPreferences prefs = getDefaultSharedPreference(Application.getInstance().getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("app_language_id", id);
editor.apply();
}
public static String getSelectedLanguageId(){
return getDefaultSharedPreference(Application.getInstance().getApplicationContext())
.getString("app_language_id", "en");
}
这三个函数应该写在Utiltiy类中(您的首选项)。 然后,当您从应用程序中选择应用程序语言时,请调用setSelectedLanguageId()函数并将语言ID作为参数传递。
这样您就可以在应用中保存所选语言。 现在在你的应用程序类中编写一个这样的函数
public void initAppLanguage(Context context){
LocaleUtils.initialize(context, PreferenceUtil.getSelectedLanguageId() );
}
这里的PreferenceUtil是我的Utiltiy类。 您应该用实用程序类函数替换它。
您还应该在应用程序类中创建一个变量
private static Application applicationInstance;
在Application类的onCreate方法中,将applicationInstance初始化为这样的应用程序上下文
applicationInstance = this;
现在在应用程序类中编写一个getter函数
public static synchronized Application getInstance() {
return applicationInstance;
}
现在,当您开始第一个活动时,请在您的活动的onCreate中调用此方法
Application.getInstance().initAppLanguage(this);
请记住,我们将活动的上下文传递给initAppLanguage()函数,而不是应用程序上下文。 传递应用程序上下文不会使它在奥利奥(至少对我来说)有效。
因此,当您选择语言时,请尝试完全重新启动应用程序。 你可以通过这个来实现
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
startActivity(ⅰ);
希望这对你有所帮助!
有可能,但我不建议以编程方式设置语言
Android的设计使得系统UI和您的应用程序具有相同的语言 ,如果您以编程方式更改它,您将与系统作斗争
相反,您可以通过添加不同的strings.xml语言来启用多语言支持,这将自动更改语言
我推荐阅读这篇Google Developers文章:
如果您确实需要以编程方式更改它,则可以执行以下操作
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
在SDK> = 21上,您需要调用'Resources.updateConfiguration()' ,否则将不会更新资源。
希望能帮助到你。
这里是kitkat,Lollipop,Marshmallow,Nougat和Oreo的完整解决方案。 请按照以下所有步骤操作。
首先创建一个如下所示的java类
import android.content.Context;
import android.content.res.Configuration;
import java.util.Locale;
public class LocaleUtils {
public static void updateConfig(Context mContext, String sLocale) {
Locale locale = new Locale(sLocale);
Locale.setDefault(locale);
Configuration config = mContext.getResources().getConfiguration();
config.locale = locale;
mContext.getResources().updateConfiguration(config,
mContext.getResources().getDisplayMetrics());
}
}
现在,在按钮上添加此片段,然后单击要更改区域设置的位置
String lang="hi";//pass your language here
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.putString("lang", lang");
editor.putBoolean("langSelected", true);
editor.apply();
LocaleUtils.updateConfig(mContext,lang);
Intent intent = mContext.getIntent();
mContext.overridePendingTransition(0, 0);
mContext.finish();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.overridePendingTransition(0, 0);
mContext.startActivity(intent);
最后将以下代码粘贴到Splash Activity或Launching Activity中。
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String lang = preferences.getString("lang", "");
boolean langSelected = preferences.getBoolean("langSelected", false);
SharedPreferences.Editor editor = preferences.edit();
if (langSelected) {
editor.clear();
editor.putString("lang", lang);
editor.putBoolean("langSelected", true);
editor.apply();
LocaleUtils.updateConfig(this,lang);
} else {
LocaleUtils.updateConfig(this, Locale.getDefault().getLanguage());
editor.clear();
editor.putString("lang", Locale.getDefault().getLanguage());
editor.putBoolean("langSelected", false);
editor.apply();
}
你需要使用getApplicationContext()
而不是getContext()
在使用所有来源的所有解决方案后,我终于找到了我的问题 这让我生气了2天。
每个人都知道在Android Oreo(API 26)中我们必须使用createConfigurationContext
,但我的问题是使用国家名称和本地。
更换
en_US with en
ar_AE with ar
fa_IR with fa
我的问题解决了。
希望能帮助别人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.