美文网首页
Android 切换系统语言

Android 切换系统语言

作者: 一个不安分的Android开发 | 来源:发表于2021-01-24 22:06 被阅读0次

切换系统语言分为下面两个步骤:

1. 创建不同语言资源;
2. 替换当前页面 Context 所持有的资源;

一、创建不同语言资源

创建步骤如下:

创建资源1.png 创建资源2.png 创建资源3.png 创建资源4.png

二、替换资源

  • 界面需要重新创建,使用 recreate(); 或者 重新打开界面。
  • 在 attachBaseContext(Context) 方法中替换Context的资源。
  //可以在 BaseActivity 中使用
  @Override
  protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(Utils.attachBaseContext(newBase));
  }
  • 切换配置

  public static Context attachBaseContext(Context context) {
    //不同版本设置方式不一样
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      return createResources(context);
    } else {
      updateResources(context);
      return context;
    }
  }

  @TargetApi(Build.VERSION_CODES.N)
  private static Context createResources(Context context) {
    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration configuration = resources.getConfiguration();
    //获取语言设置,一般用户设置的语言优先级更高,如果用户没有设置,则获取系统语言
    Locale targetLocale = getLocale(context);
    configuration.setLocale(targetLocale);
    resources.updateConfiguration(configuration, dm);
    //创建配置
    return context.createConfigurationContext(configuration);
  }

  public static void updateResources(Context pContext) {
    Locale targetLocale = getLocale(pContext);
    Configuration configuration = pContext.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      configuration.setLocale(targetLocale);
    } else {
      configuration.locale = targetLocale;
    }
    Resources resources = pContext.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    //更新配置
    resources.updateConfiguration(configuration, dm);
  }

  • 获取系统语言
  public static Locale getSystemLocale(Context context) {
    Locale locale;
    //7.0有多语言设置获取顶部的语言
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      locale = LocaleList.getDefault().get(0);
    } else {
      locale = context.getResources().getConfiguration().locale;
    }
    return locale;
  }

注意:
不能使用 Application 的 Context 来获取文字资源,必须使用 Activity 的 Context;

相关文章

  • Android设置语言

    应用语言的切换 单纯的切换自身应用的语言。 系统语言的切换 切换整个系统的语言。在6.0的系统中,切换系统语言的方...

  • Android 切换系统语言

    切换系统语言分为下面两个步骤: 1. 创建不同语言资源;2. 替换当前页面 Context 所持有的资源; 一、创...

  • [转]兼容多语言切换总结

    我们手机升级到android 7.0或购买到最新的android(大于7.0)机后,我们突然发现在系统设置语言切换...

  • android 语言切换导致的crash

    android 切换语言时候需要注意的问题 android 切换语言时候容易导致空指针的crash,为什么呢? 1...

  • Android应用实现语言切换

    语言切换需求 应用内切换语言,支持阿拉伯语(从右到左书写,即RTL语言支持) 语言切换是切换系统语言,不只本应用 ...

  • Android修改系统设置后Activity被重新创建

    前言 Android在进行横竖屏切换、修改系统语言等操作后,Activty会被自动销毁并重建。这样可以便于应用重新...

  • Android App 多语言切换

    Android App 多语言切换:http://laobie.github.io/android/2016/05...

  • 安卓系统语言切换流程

    android system language change 流程 切换语言核心过程分析 updateLocal...

  • iOS 正确的获取系统语言

    常用的获取系统语言的方法 缺点:当用户频繁切换系统语言的时候会获取错误 正确的获取系统语言的姿势

  • Android切换app语言

    效果图如下: 主要代码: 需要一提的是,要界面立即刷新效果,需要重启界面,在这里需要在选择语言对话框点击确定之后把...

网友评论

      本文标题:Android 切换系统语言

      本文链接:https://www.haomeiwen.com/subject/jcevzktx.html