下面列出了android.content.res.Configuration#setLayoutDirection() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressLint("ObsoleteSdkInt")
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, Locale locale) {
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;
}
/**
* This is public to allow for an activity to force the
* current locale to be applied if necessary (e.g., when
* a new activity launches).
*/
public void updateConfiguration(Context context, Locale locale) {
Resources res = context.getResources();
Configuration config = res.getConfiguration();
// We should use setLocale, but it's unexpectedly missing
// on real devices.
config.locale = locale;
config.setLayoutDirection(locale);
res.updateConfiguration(config, null);
}
private void updateLocale(@NonNull Context context, @NonNull Locale locale) {
final Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
config.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLayoutDirection(config.locale);
}
resources.updateConfiguration(config, resources.getDisplayMetrics());
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, Locale locale) {
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
public static void checkLanguage(Context context) {
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
config.locale = getLocal(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLayoutDirection(config.locale);
}
resources.updateConfiguration(config, dm);
}
/**
* This is public to allow for an activity to force the
* current locale to be applied if necessary (e.g., when
* a new activity launches).
*/
public void updateConfiguration(Context context, Locale locale) {
Resources res = context.getResources();
Configuration config = res.getConfiguration();
// We should use setLocale, but it's unexpectedly missing
// on real devices.
config.locale = locale;
config.setLayoutDirection(locale);
res.updateConfiguration(config, null);
}
@SuppressLint("NewApi")
private void setConfigurationLocale(Configuration config, Locale loc) {
config.locale = loc;
if (Build.VERSION.SDK_INT >= 17) {
// Don't use setLocale() in order not to trigger userSetLocale
config.setLayoutDirection(loc);
}
}
@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;
configuration.setLayoutDirection(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
/**
*
* @param context
* @return true if the application locale changed
*/
static boolean setAppLocale(Context context, Locale newLocale) {
Resources resources = context.getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
Configuration configuration = resources.getConfiguration();
Locale oldLocale = new Locale(configuration.locale.getLanguage(), configuration.locale.getCountry());
configuration.locale = newLocale;
// Sets the layout direction from the Locale
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sLogger.debug("Setting the layout direction");
configuration.setLayoutDirection(newLocale);
}
resources.updateConfiguration(configuration, displayMetrics);
if(oldLocale.equals(newLocale)) {
return false;
}
if (LocalesUtils.updatePreferredLocale(newLocale)) {
sLogger.info("Locale preferences updated to: " + newLocale);
Locale.setDefault(newLocale);
} else {
sLogger.error("Failed to update locale preferences.");
}
return true;
}
public static void setContextLocale(Context context, Locale selectedLocale) {
Configuration configuration = context.getResources().getConfiguration();
if (!configuration.locale.equals(selectedLocale)) {
configuration.locale = selectedLocale;
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(selectedLocale);
}
context.getResources().updateConfiguration(configuration,
context.getResources().getDisplayMetrics());
}
}
private static void setContextLocale(Context context, Locale selectedLocale) {
Configuration configuration = context.getResources().getConfiguration();
if (!configuration.locale.equals(selectedLocale)) {
configuration.locale = selectedLocale;
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(selectedLocale);
}
context.getResources().updateConfiguration(configuration,
context.getResources().getDisplayMetrics());
}
}
private void setLocaleWith(Locale locale) throws
ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
Class<?> activityManagerNativeClass = Class.forName("android.app.ActivityManagerNative");
Method methodGetDefault = activityManagerNativeClass.getMethod("getDefault");
methodGetDefault.setAccessible(true);
Object amn = methodGetDefault.invoke(activityManagerNativeClass);
// Build.VERSION_CODES.O
if (Build.VERSION.SDK_INT >= 26) {
// getConfiguration moved from ActivityManagerNative to ActivityManagerProxy
activityManagerNativeClass = Class.forName(amn.getClass().getName());
}
Method methodGetConfiguration = activityManagerNativeClass.getMethod("getConfiguration");
methodGetConfiguration.setAccessible(true);
Configuration config = (Configuration) methodGetConfiguration.invoke(amn);
Class<?> configClass = config.getClass();
Field f = configClass.getField("userSetLocale");
f.setBoolean(config, true);
config.locale = locale;
config.setLayoutDirection(locale);
Method methodUpdateConfiguration = activityManagerNativeClass.getMethod("updateConfiguration", Configuration.class);
methodUpdateConfiguration.setAccessible(true);
methodUpdateConfiguration.invoke(amn, config);
}
public static Context setLocale(Context context, String locale) {
setPreferredLocale(context, locale);
Locale newLocale = (locale.isEmpty()) ? SYSTEM_DEFAULT_LOCALE : Locale.forLanguageTag(locale);
Configuration configuration = context.getResources().getConfiguration();
Locale.setDefault(newLocale);
configuration.setLocale(newLocale);
configuration.setLayoutDirection(newLocale);
return context.createConfigurationContext(configuration);
}