Android 切换系统语言

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

1. 创建不同语言资源;

2. 替换当前页面 Context 所持有的资源;

一、创建不同语言资源

创建步骤如下:

二、替换资源

  • 界面需要重新创建,使用 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;

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android切换语言,可以通过设置系统语言或者应用内部语言来实现。下面介绍两种常用的方法: 1. 设置系统语言: 在AndroidManifest.xml文件中,可以添加以下代码: ```xml <manifest ...> <application ... android:supportsRtl="true" android:allowBackup="true" android:theme="@style/AppTheme"> <activity ... android:configChanges="locale" android:screenOrientation="portrait" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ``` 然后在Application类中,重写attachBaseContext方法: ```java public class MyApplication extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(LocaleHelper.onAttach(base, "en")); // 设置默认语言为英文 } } ``` 在需要切换语言的地方,可以调用以下代码: ```java LocaleHelper.setLocale(context, "zh"); // 设置语言为中文 recreate(); // 重启Activity生效 ``` 2. 设置应用内部语言: 使用SharedPreferences来保存用户选择的语言,然后在需要切换语言的地方,可以调用以下代码: ```java String language = "zh"; // 用户选择的语言,例如中文("zh")或英文("en") LocaleHelper.setLocale(context, language); recreate(); // 重启Activity生效 ``` 以上是两种常用的切换语言的方法,你可以根据具体需求选择适合的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值