就在最近的context.getResources().updateConfiguration()已在Android API 25中弃用,建议使用上下文.而是createConfigurationContext().
有谁知道createConfigurationContext如何用于覆盖android系统区域设置?
在此之前将通过以下方式完成:
Configuration config = getBaseContext().getResources().getConfiguration();
config.setLocale(locale);
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
Run Code Online (Sandbox Code Playgroud) 我发现非常奇怪的错误只能在Android N设备上重现.
在浏览我的应用程序时,有可能改变语言.这是改变它的代码.
public void update(Locale locale) {
Locale.setDefault(locale);
Configuration configuration = res.getConfiguration();
if (BuildUtils.isAtLeast24Api()) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
configuration.setLocale(locale);
} else if (BuildUtils.isAtLeast17Api()){
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
res.updateConfiguration(configuration, res.getDisplayMetrics());
}
Run Code Online (Sandbox Code Playgroud)
这段代码非常适合我的游览活动(通过recreate()调用),但在所有下一个活动中,所有String资源都是错误的.屏幕旋转修复它.我该怎么办这个问题?我应该以不同方式更改Android N的区域设置,还是只是系统错误?
PS这是我发现的.首次启动MainActivity(在我的巡演之后)Locale.getDefault()是正确的,但资源是错误的.但在其他活动中,它给了我错误的Locale和来自此语言环境的错误资源.旋转屏幕(或可能是其他一些配置更改)Locale.getDefault()是正确的.
我想改变应用程序的语言,直到API 26才能正常工作.
对于api> 25我Locale.setDefault(Locale.Category.DISPLAY, mynewlanglocale);之前提出setContentView(R.layout.activity_main);但没有任何变化.
该文档不解释太多关于这一点.
这是问题所在:当我在后台运行一个活动,并切换区域设置,然后切换回应用程序时,所有内容都会更新... EXCEPT复选框和具有"android:id"属性集的单选按钮.
如果复选框和单选按钮没有"android:id"属性,则它们会更新OK.其他字段没有这个问题,是否具有"android:id"属性.
在更改语言环境时,确保运行活动中的所有内容都更新的最佳方法是什么?
重现步骤:
1)在Eclipse中创建一个"Hello,Android"项目.2)在主布局中,定义两个复选框:
<CheckBox android:text="@string/checkbox" android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<CheckBox android:text="@string/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
Run Code Online (Sandbox Code Playgroud)
3)创建两个strings.xml:一个在"values"下,一个在"values-es"下.
4)在"values"下创建以下字符串:
<string name="checkbox">English</string>
Run Code Online (Sandbox Code Playgroud)
5)在"values-es"下创建以下字符串
<string name="checkbox">español</string>
Run Code Online (Sandbox Code Playgroud)
6)将设备设置为"英语"
7)在仿真器或任何设备上运行应用程序(在HTC G1上测试).
8)观察.两个复选框都说"英语".
9)按"Home"返回菜单,让应用程序在后台运行.
10)转到设置.将语言切换为"español"
11)按住"Home".返回申请.
预期结果:
两个复选框都说"español"
实际结果:
第一个复选框说"英语"
第二个复选框说"español"
看来带有"android:id"属性的复选框没有按预期更新.没有"android:id"属性的复选框正在按预期工作.
自从我在移动设备上升级到android oreo后,我的RTL支持应用程序无效.它正在将字符串更改为阿拉伯语,但不会更改布局方向.但如果我将同一个RTL转移到低于oreo的任何设备,一切正常.其他人遇到过这个问题?关于这个bug和解决方法还有任何官方声明吗?
下面是我更改区域设置的方法
public static boolean setDefaultLocale(Context context) {
Resources resources = context.getResources();
PreferenceManager preferenceManager = PreferenceManager.getInstance();
String localLanguage = resources.getConfiguration().locale.getLanguage();
boolean isLanguageChanged = !preferenceManager.getCurrentLanguageCode().equalsIgnoreCase(localLanguage);
if (isLanguageChanged) {
Log.d("", preferenceManager.getCurrentLanguageCode());
Locale locale = new Locale(preferenceManager.getCurrentLanguageCode());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
Locale.setDefault(Locale.Category.DISPLAY, locale);
else
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
resources.updateConfiguration(config, resources.getDisplayMetrics());
((Activity) context).recreate();
}
return isLanguageChanged;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码在我的应用程序中设置特定语言.语言保存SharedPreferences在应用程序中.它完全可以达到API级别23.由于Android N也SharedPreferences运行良好,它返回正确的语言代码字符串,但它不会更改区域设置(设置手机的默认语言).可能有什么不对?
更新1:当我Log.v("MyLog", config.locale.toString());在res.updateConfiguration(config, dm)它返回正确的语言环境后立即使用,但应用程序的语言没有改变.
更新2:我还提到如果我更改区域设置然后重新启动活动(使用新意图并完成旧版本),它会正确更改语言,甚至在轮换后显示正确的语言.但是当我关闭应用程序并再次打开它时,我会得到默认语言.有点奇怪.
public class ActivityMain extends AppCompatActivity {
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set locale
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
String lang = pref.getString(ActivityMain.LANGUAGE_SAVED, "no_language");
if (!lang.equals("no_language")) {
Resources res = context.getResources();
Locale locale = new Locale(lang);
Locale.setDefault(locale);
DisplayMetrics dm = res.getDisplayMetrics();
Configuration config = res.getConfiguration();
if (Build.VERSION.SDK_INT >= 17) {
config.setLocale(locale);
} else {
config.locale = locale;
}
}
res.updateConfiguration(config, …Run Code Online (Sandbox Code Playgroud) 我试图在运行时更改应用程序的语言环境。在API级别24以下的Andorid中,它可以正常工作。但是在API级别24或更高版本中,布局方向不会根据语言环境而变化。下面是在运行时更改语言环境的代码。我使用了如下的LocaleHelper类
public class LocaleHelper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static Context onAttach(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
return setLocale(context, lang);
}
public static Context onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static Context setLocale(Context context, String language) {
persist(context, language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}
return updateResourcesLegacy(context, language);
}
private static String …Run Code Online (Sandbox Code Playgroud) android locale localization android-layout android-7.0-nougat