android.content.res.Resources#updateConfiguration()源码实例Demo

下面列出了android.content.res.Resources#updateConfiguration() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: focus-android   文件: SwitchLocaleTest.java
@SuppressWarnings("deprecation")
public static void changeLocale(String locale) {
    Context context = InstrumentationRegistry.getInstrumentation()
            .getTargetContext();

    Resources res = context.getApplicationContext().getResources();
    Configuration config = res.getConfiguration();


    config.setLocale(new Locale(locale));
    if (SDK_INT >= 25) {
        context.createConfigurationContext(config);
    } else {
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
}
 
源代码2 项目: Lamp   文件: WifiConnectActivity.java
private void checkLanguage() {

        SharedPreferences sharedPreferences = this.getSharedPreferences("set", MODE_PRIVATE);
        /**
         * 获取系统配置
         */
        Resources resources = getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();


        if (LANGUAGE_CN.equals(sharedPreferences.getString("language", "0"))) {

            config.locale = Locale.SIMPLIFIED_CHINESE;
            resources.updateConfiguration(config, dm);
        } else if (LANGUAGE_EN.equals(sharedPreferences.getString("language", "0"))) {

            config.locale = Locale.ENGLISH;
            resources.updateConfiguration(config, dm);
        } else {

            config.locale = Locale.SIMPLIFIED_CHINESE;
            resources.updateConfiguration(config, dm);
        }

    }
 
源代码3 项目: PocketEOS-Android   文件: LocalManageUtil.java
/**
 * 设置语言类型
 */
public static void setApplicationLanguage(Context context) {
    Resources resources = context.getApplicationContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    Locale locale = getSetLanguageLocale(context);
    config.locale = locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context.getApplicationContext().createConfigurationContext(config);
        Locale.setDefault(locale);
    }
    resources.updateConfiguration(config, dm);
}
 
源代码4 项目: kcanotify_h5-master   文件: LocaleUtils.java
public static void updateConfig(Application app, Configuration configuration) {
    if(sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        //Wrapping the configuration to avoid Activity endless loop
        Configuration config = new Configuration(configuration);
        config.locale = sLocale;
        Resources res = app.getBaseContext().getResources();
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
}
 
源代码5 项目: RedReader   文件: PrefsUtility.java
private static void applyLanguage(final Activity activity, final SharedPreferences prefs) {

		final String lang = getString(R.string.pref_appearance_langforce_key, "auto", activity, prefs);

		for(final Resources res : new Resources[] {
			activity.getResources(),
			activity.getApplication().getResources()
		}) {

			final DisplayMetrics dm = res.getDisplayMetrics();
			final android.content.res.Configuration conf = res.getConfiguration();

			if(!lang.equals("auto")) {

				if(lang.contains("-r")) {
					final String[] split = lang.split("-r");
					conf.locale = new Locale(split[0], split[1]);

				} else {
					conf.locale = new Locale(lang);
				}

			} else {
				conf.locale = Locale.getDefault();
			}

			res.updateConfiguration(conf, dm);
		}
	}
 
源代码6 项目: quickhybrid-android   文件: FrmApplication.java
@Override
public Resources getResources() {
    Resources res = super.getResources();
    if (res.getConfiguration().fontScale != 1) {//非默认值
        Configuration newConfig = new Configuration();
        newConfig.setToDefaults();//设置默认
        res.updateConfiguration(newConfig, res.getDisplayMetrics());
    }
    return res;
}
 
源代码7 项目: MagicaSakura   文件: ThemeUtils.java
public static Resources updateNightMode(Resources resource, boolean on) {
    DisplayMetrics dm = resource.getDisplayMetrics();
    Configuration config = resource.getConfiguration();
    final int uiModeNightMaskOrigin = config.uiMode &= ~Configuration.UI_MODE_TYPE_MASK;
    final int uiModeNightMaskNew = on ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
    if (uiModeNightMaskOrigin != uiModeNightMaskNew) {
        config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        config.uiMode |= uiModeNightMaskNew;
        resource.updateConfiguration(config, dm);
    }
    return resource;
}
 
源代码8 项目: UpdogFarmer   文件: LocaleManager.java
private static Context updateResources(Context context, String language) {
    final Locale locale = forLanguageTag(language);
    Locale.setDefault(locale);

    final Resources res = context.getResources();
    final Configuration config = new Configuration(res.getConfiguration());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(locale);
        context = context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
    return context;
}
 
源代码9 项目: SimpleProject   文件: MyApplication.java
/**
 * 放大系统字体将导致布局错乱,所以一般可将其屏蔽
 * @return
 */
@Override
public Resources getResources() {
	Resources res = super.getResources();
	if (res.getConfiguration().fontScale != 1) {
		Configuration newConfig = new Configuration();
		newConfig.setToDefaults();
		res.updateConfiguration(newConfig, res.getDisplayMetrics());
	}

	return res;
}
 
源代码10 项目: letv   文件: ProxyFragment.java
private void updateConfiguration() {
    if (JarApplication.getInstance() != null && JarApplication.getInstance().getResources() != null) {
        Resources superRes = JarApplication.getInstance().getResources();
        Configuration configuration = superRes.getConfiguration();
        DisplayMetrics displayMetrics = superRes.getDisplayMetrics();
        JarResources jarResources = getOverrideResources();
        if (jarResources != null) {
            Resources resources = jarResources.getResources();
            if (resources != null && configuration != null && displayMetrics != null) {
                resources.updateConfiguration(configuration, displayMetrics);
            }
        }
    }
}
 
源代码11 项目: OpenHub   文件: AppUtils.java
@SuppressWarnings("deprecation")
private static void updateResourcesLegacy(Context context, String language) {
    Locale locale = getLocale(language);
    Locale.setDefault(locale);
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
 
源代码12 项目: openshop.io-android   文件: MyApplication.java
/**
 * Method sets app specific language localization by selected shop.
 * Have to be called from every activity.
 *
 * @param lang language code.
 */
public static void setAppLocale(String lang) {
    Resources res = mInstance.getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(lang);
    Timber.d("Setting language: %s", lang);
    res.updateConfiguration(conf, dm);
}
 
源代码13 项目: Augendiagnose   文件: Application.java
/**
 * Create a ContextWrapper, wrappint the context with a specific locale.
 *
 * @param context The original context.
 * @return The context wrapper.
 */
public static ContextWrapper createContextWrapperForLocale(final Context context) {
	Resources res = context.getResources();
	Configuration configuration = res.getConfiguration();
	Locale newLocale = getApplicationLocale();
	Context newContext = context;

	if (VERSION.SDK_INT >= VERSION_CODES.N) {
		configuration.setLocale(newLocale);

		LocaleList localeList = new LocaleList(newLocale);
		LocaleList.setDefault(localeList);
		configuration.setLocales(localeList);

		newContext = context.createConfigurationContext(configuration);

	}
	else if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
		configuration.setLocale(newLocale);
		newContext = context.createConfigurationContext(configuration);

	}
	else {
		configuration.locale = newLocale;
		res.updateConfiguration(configuration, res.getDisplayMetrics());
	}
	return new ContextWrapper(newContext);
}
 
源代码14 项目: SmartFlasher   文件: Utils.java
public static void setLanguage(Context context) {
    Locale myLocale = new Locale(getLanguage(context));
    Resources res = context.getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
}
 
源代码15 项目: AndroidMultiLanguage   文件: MultiLanguageUtil.java
/**
 * 设置语言
 */
public void setConfiguration() {
    Locale targetLocale = getLanguageLocale();
    Configuration configuration = mContext.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(targetLocale);
    } else {
        configuration.locale = targetLocale;
    }
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
}
 
源代码16 项目: BigApp_Discuz_Android   文件: AppUtils.java
/**
 * 改变语言环境
 *
 * @param resources
 * @param lanAtr
 */
public static void changeAppLanguage(Resources resources, String lanAtr) {
    Configuration config = resources.getConfiguration();
    DisplayMetrics dm = resources.getDisplayMetrics();
    if (lanAtr.equals("zh-cn")) {
        config.locale = Locale.SIMPLIFIED_CHINESE;
    } else {
        config.locale = Locale.getDefault();
    }
    resources.updateConfiguration(config, dm);
}
 
源代码17 项目: Album   文件: AlbumUtils.java
/**
 * Setting {@link Locale} for {@link Context}.
 *
 * @param context to set the specified locale context.
 * @param locale  locale.
 */
@NonNull
public static Context applyLanguageForContext(@NonNull Context context, @NonNull Locale locale) {
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocale(locale);
        return context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        return context;
    }
}
 
源代码18 项目: MaterialPreference   文件: MainActivity.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_night_mode:
            /*AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES ?
                    AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES);*/

            recreate();

            return true;
        case R.id.rtl:
            Resources resources = getBaseContext().getResources();

            Locale locale = resources.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL
                    ? Locale.ENGLISH : new Locale("ar");
            Locale.setDefault(locale);

            resources.getConfiguration().setLocale(locale);
            resources.updateConfiguration(resources.getConfiguration(), resources.getDisplayMetrics());

            startActivity(new Intent(this, this.getClass()));
            finish();

            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
public static Context updateContext(Context context, String language) {
  final Locale newLocale = LocaleParser.findBestMatchingLocaleForLanguage(language);

  Locale.setDefault(newLocale);

  final Resources     resources = context.getResources();
  final Configuration config    = resources.getConfiguration();
  final Configuration newConfig = copyWithNewLocale(config, newLocale);

  resources.updateConfiguration(newConfig, resources.getDisplayMetrics());

  return context;
}
 
源代码20 项目: test-butler   文件: TestButler.java
/**
 * Set the locale for the application under test. Requires API 17+.
 * <p>
 * NOTE: this does not change the device locale! Changing the device locale on the emulator
 * has been slow/unreliable and led to flaky tests when the locale setting did not take effect in time.
 * Instead, this method uses the {@link Configuration} class to update the locale on the {@link Resources}
 * object of the {@link Context} from the application under test. As long as apps are checking the
 * current locale from the current {@link Configuration}, this approach will be reliable.
 *
 * @param language the language code for the new locale, as expected by {@link Locale#Locale(String, String)}
 * @param country  the country code for the new locale, as expected by {@link Locale#Locale(String, String)}
 * @param context  the "target context"; i.e. Context of the app under test (not the test apk context!)
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void setLocale(@NonNull String language, @NonNull String country, @NonNull Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        throw new IllegalStateException("Cannot change locale before API 17");
    }

    context = context.getApplicationContext();
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(new Locale(language, country));
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}