android.content.res.Configuration.setLocale()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(165)

本文整理了Java中android.content.res.Configuration.setLocale()方法的一些代码示例,展示了Configuration.setLocale()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration.setLocale()方法的具体详情如下:
包路径:android.content.res.Configuration
类名称:Configuration
方法名:setLocale

Configuration.setLocale介绍

暂无

代码示例

代码示例来源:origin: robolectric/robolectric

private static void setLocale(int apiLevel, Configuration configuration, Locale locale) {
 if (apiLevel >= VERSION_CODES.JELLY_BEAN_MR1) {
  configuration.setLocale(locale);
 } else {
  configuration.locale = locale;
 }
}

代码示例来源:origin: seven332/EhViewer

public static ContextLocalWrapper wrap(Context context, Locale newLocale) {
  Resources res = context.getResources();
  Configuration configuration = res.getConfiguration();

  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 ContextLocalWrapper(context);
 }
}

代码示例来源:origin: facebook/litho

private static void setLocale(Configuration configuration, Locale locale) {
  if (Build.VERSION.SDK_INT >= 17) {
   configuration.setLocale(locale);
  } else {
   configuration.locale = locale;
  }
 }
}

代码示例来源:origin: ukanth/afwall

public static ContextWrapper wrap(Context context, Locale newLocale) {
  Resources res = context.getResources();
  Configuration configuration = res.getConfiguration();
  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);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = Build.VERSION_CODES.JELLY_BEAN_MR1)
public void testSetLocale() {
 configuration.setLocale( Locale.US );
 assertThat(configuration.locale).isEqualTo(Locale.US);
 configuration.setLocale( Locale.FRANCE);
 assertThat(configuration.locale).isEqualTo(Locale.FRANCE);
}

代码示例来源:origin: robolectric/robolectric

private void checkForPollutionHelper() {
 assertThat(RuntimeEnvironment.getQualifiers())
   .isEqualTo("en-rUS-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-" + optsForO + "port-notnight-mdpi-finger-keyssoft-nokeys-navhidden-nonav-v" + Build.VERSION.RESOURCES_SDK_INT);
 View view =
   LayoutInflater.from(ApplicationProvider.getApplicationContext())
     .inflate(R.layout.different_screen_sizes, null);
 TextView textView = view.findViewById(android.R.id.text1);
 assertThat(textView.getText().toString()).isEqualTo("default");
 RuntimeEnvironment.setQualifiers("fr-land"); // testing if this pollutes the other test
 Configuration configuration = Resources.getSystem().getConfiguration();
 if (RuntimeEnvironment.getApiLevel() <= VERSION_CODES.JELLY_BEAN) {
  configuration.locale = new Locale("fr", "FR");
 } else {
  configuration.setLocale(new Locale("fr", "FR"));
 }
 configuration.orientation = Configuration.ORIENTATION_LANDSCAPE;
 Resources.getSystem().updateConfiguration(configuration, null);
}

代码示例来源:origin: ukanth/afwall

@TargetApi(Build.VERSION_CODES.N)
private static Context updateResourcesLocale(Context context, Locale locale) {
  Configuration configuration = context.getResources().getConfiguration();
  configuration.setLocale(locale);
  return context.createConfigurationContext(configuration);
}

代码示例来源:origin: westnordost/StreetComplete

private Resources getResources(Locale locale)
  {
    Configuration configuration = new Configuration(applicationContext.getResources().getConfiguration());
    configuration.setLocale(locale);
    return applicationContext.createConfigurationContext(configuration).getResources();
  }
}

代码示例来源:origin: ukanth/afwall

private static Context updateResources(Context context, String language) {
  Locale locale = new Locale(language);
  Locale.setDefault(locale);
  Resources res = context.getResources();
  Configuration config = new Configuration(res.getConfiguration());
  if (Build.VERSION.SDK_INT >= 17) {
    config.setLocale(locale);
    context = context.createConfigurationContext(config);
  } else {
    config.locale = locale;
    res.updateConfiguration(config, res.getDisplayMetrics());
  }
  return context;
}

代码示例来源:origin: DaxiaK/MyDiary

private void overwriteConfigurationLocale(Configuration config, Locale locale) {
  //TODO FIX updateConfiguration on Android N
  config.setLocale(locale);
  getBaseContext().getResources()
      .updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}

代码示例来源:origin: syncthing/syncthing-android

Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= 17) {
  config.setLocale(locale);
} else {
  config.locale = locale;

代码示例来源:origin: org.robolectric/shadows-framework

private static void setLocale(int apiLevel, Configuration configuration, Locale locale) {
 if (apiLevel >= VERSION_CODES.JELLY_BEAN_MR1) {
  configuration.setLocale(locale);
 } else {
  configuration.locale = locale;
 }
}

代码示例来源:origin: mauriciotogneri/green-coffee

@TargetApi(Build.VERSION_CODES.N)
private void systemLocale(Configuration config, Locale locale)
{
  config.setLocale(locale);
}

代码示例来源:origin: VREMSoftwareDevelopment/WiFiAnalyzer

@TargetApi(Build.VERSION_CODES.N)
@NonNull
private static Context createContextNougat(@NonNull Context context, @NonNull Locale newLocale) {
  Resources resources = context.getResources();
  Configuration configuration = resources.getConfiguration();
  configuration.setLocale(newLocale);
  return context.createConfigurationContext(configuration);
}

代码示例来源:origin: WangDaYeeeeee/GeometricWeather

public static void setLanguage(Context c, String language) {
  Locale target = buildLocale(language);
  if (!c.getResources().getConfiguration().locale.equals(target)) {
    Resources resources = c.getResources();
    Configuration configuration = resources.getConfiguration();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    configuration.setLocale(target);
    resources.updateConfiguration(configuration, metrics);
  }
}

代码示例来源:origin: AndBible/and-bible

@TargetApi(Build.VERSION_CODES.N)
  @Override
  public Context changeLocale(Context context, String language) {
    logger.debug("Update resources N plus");

    Locale locale = Locale.forLanguageTag(language);
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);

    return context.createConfigurationContext(configuration);
  }
}

代码示例来源:origin: gunhansancar/ChangeLanguageExample

@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);
}

代码示例来源:origin: 8enet/AppOpsX

public static void updateLanguage(Context context) {
 Resources resources = context.getResources();
 Configuration config = resources.getConfiguration();
 config.setLocale(getLocaleByLanguage(context));
 DisplayMetrics dm = resources.getDisplayMetrics();
 resources.updateConfiguration(config, dm);
}

代码示例来源:origin: org.robolectric/shadows-core

@Deprecated
@Implementation
public void setLocale( Locale l ) {
 directlyOn(realConfiguration, Configuration.class).setLocale(l);
}

代码示例来源:origin: WangDaYeeeeee/Mysplash

public static void setLanguage(Context c) {
  String language = SettingsOptionManager.getInstance(c).getLanguage();
  if (!language.equals("follow_system")) {
    Resources resources = c.getResources();
    Configuration configuration = resources.getConfiguration();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    configuration.setLocale(getLocale(c, language));
    resources.updateConfiguration(configuration, metrics);
  }
}

相关文章