我正在开发一个需要支持多种语言的应用程序,如果语言是 RTL,我必须应用自定义字体。根据要求,我创建了 extends Application
类。一切都很完美,直到我得到 Oreo 版本的设备(在我拥有支持 Marshmellow 的设备之前)。在 Oreo 中,如果我们想要更改语言,我们必须创建一个自定义的 ContextWrapper
类,问题就来了。
- 要使用
书法
,我们需要Override
attachBaseContext
方法。和 - 要更改语言,我们还需要
Override
attachBaseContext
我尝试在 Overrided
方法中调用 super.attachBaseContext
两次 One 用于 Calligraphy 和 Other如下所示的语言代码。
@Override
protected void attachBaseContext(Context newBase) {
// create or get your new Locale object here.
String lang = Preferences
.getSharedPreferenceString(appContext, LANGUAGE_KEY, "ar");
Context context = MyContextWrapper.wrap(newBase, lang);
super.attachBaseContext(context);
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
它给出 IllegalStateException 因为我们可以附加一次基础上下文。
- 如果我使用
super.attachBaseContext(context);
语言更改有效,但 Calligraphy 无效。 - 如果我使用
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
书法有效,但语言更改无效。
在这种情况下,我怎样才能使所有工作(书法+多语言)。我已经查看了很多帖子/教程,但我现在已经坚持了三天。
请帮我完成这个。谢谢
编辑:引用
- CalligraphyContextWrapper.java
- ContextWrapper类(class)在链接中接受了答案。
寻找能够使用具有书法和更改语言功能的自定义字体的解决方案。或者提供一种方法,以便我可以更改语言并将字体应用于整个应用程序。
注意:该解决方案必须兼容 API 17 到最新的 27。我正在使用 AppCompat
。
最佳答案
奥利奥我做过
在您的 Activity 中:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(SLocaleHelper.onAttach(newBase)));
}
或
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(SLocaleHelper.onAttach(newBase)));
}
在您的应用程序主类中:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(SLocaleHelper.onAttach(base, "sv"));
MultiDex.install(this);
}
语言环境助手类:
package com.......;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import java.util.Locale;
public class SLocaleHelper {
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 getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
关于java - 如何使用多语言支持的 Oreo 书法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48926729/