Android 奥利奥本地化有时不起作用

Android 奥利奥本地化有时不起作用,android,localization,android-8.0-oreo,Android,Localization,Android 8.0 Oreo,这是一个重复的问题,但我没有从这些答案中得到解决方案,这就是为什么我发布这个问题,我正在寻找一个解决方案一个多星期 在Android中,Oreo本地化有时不起作用。所有字符串仅以设备语言显示 if (languagecode.equals("1")) { Resources res = getApplicationContext().getResources(); DisplayMetrics dm = res.g

这是一个重复的问题,但我没有从这些答案中得到解决方案,这就是为什么我发布这个问题,我正在寻找一个解决方案一个多星期

在Android中,Oreo本地化有时不起作用。所有字符串仅以设备语言显示

 if (languagecode.equals("1")) {
                    Resources res = getApplicationContext().getResources();
                    DisplayMetrics dm = res.getDisplayMetrics();
                    android.content.res.Configuration conf = 
                    res.getConfiguration();
                    conf.locale = new Locale("ml");
                    res.updateConfiguration(conf, dm);

                    txt_details.setText(R.string.card_det);
                    txt_no.setText(R.string.card_number);


                }
 if (languagecode.equals("2")) {
                    Resources res = getApplicationContext().getResources();
                    DisplayMetrics dm = res.getDisplayMetrics();
                    android.content.res.Configuration conf = 
                    res.getConfiguration();
                    conf.locale = new Locale("ta");
                    res.updateConfiguration(conf, dm);

                    txt_details.setText(R.string.card_det);
                    txt_no.setText(R.string.card_number);


                }
我试过了


这些答案没有给出解决方案,请帮助我

这段代码在过去对我很有用,它来自我参与的一个令人惊叹的项目:

可能您遗漏了
if
语句中的内容:

public void setLocale(String localeString) {
  Resources res = getResources();
  Configuration conf = res.getConfiguration();
  Locale locale = new Locale(localeString);
  Locale.setDefault(locale);
  if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
    conf.setLocale(locale);
    getApplicationContext().createConfigurationContext(conf);
  }

  DisplayMetrics dm = res.getDisplayMetrics();
  if (VERSION.SDK_INT >= VERSION_CODES.N) {
    conf.setLocales(new LocaleList(locale));
  } else {
    conf.locale = locale;
  }
  res.updateConfiguration(conf, dm);
}
无论如何,不要复制代码,而是使用所需的本地字符串调用
setLocale()
方法:)