安卓APP内切换语言,无需重新启动APP(兼容7.0以下版本)

第一步 将要切换的语言保存在本地:我这里使用SharedPreferences将三种语言保存起来:en,zh,ru

第二步 我们写一个BaseActivity类,将它继承给每个activity:

/**
   * 此方法先于 onCreate()方法执行
   * @param newBase
   */
  @Override
  protected void attachBaseContext(Context newBase) {
    String language;
    if(!TextUtils.isEmpty(SPUtils.getLanguage())) {
      language = SPUtils.getLanguage();
    } else {
      Locale locale = Locale.getDefault();
      language = locale.getLanguage();
    }
    Context context = LanguageUtil.attachBaseContext(newBase, language);
    //此处的作用是:appcompat升级1.2版本之后,要加上此方法,原因就在于AppCompatActivity中获取的Resources 并非我们设置切换目标语言的Resources,而是 appcompat框架额外包裹一层ContextThemeWrapper中的 Resources。
    final Configuration configuration = context.getResources().getConfiguration();
    final ContextThemeWrapper wrappedContext = new ContextThemeWrapper(context,
            R.style.Theme_AppCompat_Empty) {
      @Override
      public void applyOverrideConfiguration(Configuration overrideConfiguration) {
        if (overrideConfiguration != null) {
          overrideConfiguration.setTo(configuration);
        }
        super.applyOverrideConfiguration(overrideConfiguration);
      }
    };
    super.attachBaseContext(wrappedContext);
  }

第三步 创建LanguageUtil 方法:

public class LanguageUtil {

    /**
     * @param context
     * @param newLanguage 想要切换的语言类型 比如 "en" ,"zh"
     */
    public static void changeAppLanguage(Context context, String newLanguage) {
        if (TextUtils.isEmpty(newLanguage)) {
            return;
        }
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale locale = getLocaleByLanguage(newLanguage);
        configuration.setLocale(locale);
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);
    }

    public static Locale getLocaleByLanguage(String language) {
        Locale locale = Locale.SIMPLIFIED_CHINESE;
        if (language.equals(LanguageType.CHINESE.getLanguage())) {
            locale = Locale.SIMPLIFIED_CHINESE;
        } else if (language.equals(LanguageType.ENGLISH.getLanguage())) {
            locale = Locale.ENGLISH;
        } else if (language.equals(LanguageType.Russian.getLanguage())) {
            locale = Locale.forLanguageTag(language);
        }
        return locale;
    }

    public static Context attachBaseContext(Context context, String language) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language);
        } else {
            return context;
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language) {
        Resources resources = context.getResources();
        Locale locale = LanguageUtil.getLocaleByLanguage(language);
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        configuration.setLocales(new LocaleList(locale));
        return context.createConfigurationContext(configuration);
    }
}

第四步:LanguageType当中定义的就是自定义的语言类型,可根据自己需求更改

public enum LanguageType {

    CHINESE("zh"),
    ENGLISH("en"),
    Russian("ru");

    private String language;

    LanguageType(String language) {
        this.language = language;
    }

    public String getLanguage() {
        return language == null ? "" : language;
    }
}

第五步 对于低版本手机小于7.0:

/*
     * 对于7.0以下,需要在Application创建的时候进行语言切换
     */
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
      String language = null;
      if(!TextUtils.isEmpty(SPUtils.getLanguage())) {
        language = SPUtils.getLanguage();
      }
      LanguageUtil.changeAppLanguage(MainApplication.getAppContext(), language);
    }
本人第一次写,可能写的不太好,大神们请多多指教 。。。。。。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值