android切换码率不起作用,解决Android 7.0 App内切换语言不生效的问题

博客给出了一个Android应用语言切换工具类AppLanguageUtils的代码。该类包含语言映射、切换应用语言、判断支持语言、获取支持语言和Locale信息等方法,可根据不同版本的Android系统进行语言设置和配置更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 public classAppLanguageUtils {2

3 public static HashMap mAllLanguages = new HashMap(8) {{4 put(Constants.ENGLISH, Locale.ENGLISH);5 put(Constants.CHINESE, Locale.SIMPLIFIED_CHINESE);6 put(Constants.SIMPLIFIED_CHINESE, Locale.SIMPLIFIED_CHINESE);7 put(Constants.TRADITIONAL_CHINESE, Locale.TRADITIONAL_CHINESE);8 put(Constants.FRANCE, Locale.FRANCE);9 put(Constants.GERMAN, Locale.GERMANY);10 put(Constants.HINDI, new Locale(Constants.HINDI, "IN"));11 put(Constants.ITALIAN, Locale.ITALY);12 }};13

14 @SuppressWarnings("deprecation")15 public static voidchangeAppLanguage(Context context, String newLanguage) {16 Resources resources =context.getResources();17 Configuration configuration =resources.getConfiguration();18

19 //app locale

20 Locale locale =getLocaleByLanguage(newLanguage);21

22 if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.JELLY_BEAN_MR1) {23 configuration.setLocale(locale);24 } else{25 configuration.locale =locale;26 }27

28 //updateConfiguration

29 DisplayMetrics dm =resources.getDisplayMetrics();30 resources.updateConfiguration(configuration, dm);31 }32

33

34 private staticboolean isSupportLanguage(String language) {35 returnmAllLanguages.containsKey(language);36 }37

38 public staticString getSupportLanguage(String language) {39 if(isSupportLanguage(language)) {40 returnlanguage;41 }42

43 if (null == language) {//为空则表示首次安装或未选择过语言,获取系统默认语言

44 Locale locale =Locale.getDefault();45 for(String key : mAllLanguages.keySet()) {46 if (TextUtils.equals(mAllLanguages.get(key).getLanguage(), locale.getLanguage())) {47 returnlocale.getLanguage();48 }49 }50 }51 returnConstants.ENGLISH;52 }53

54 /**55 * 获取指定语言的locale信息,如果指定语言不存在{@link #mAllLanguages},返回本机语言,如果本机语言不是语言集合中的一种{@link #mAllLanguages},返回英语56 *57 * @param language language58 * @return59 */

60 public staticLocale getLocaleByLanguage(String language) {61 if(isSupportLanguage(language)) {62 return mAllLanguages.get(language);63 } else{64 Locale locale =Locale.getDefault();65 for(String key : mAllLanguages.keySet()) {66 if (TextUtils.equals(mAllLanguages.get(key).getLanguage(), locale.getLanguage())) {67 returnlocale;68 }69 }70 }71 returnLocale.ENGLISH;72 }73

74

75 public staticContext attachBaseContext(Context context, String language) {76 if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.N) {77 returnupdateResources(context, language);78 } else{79 returncontext;80 }81 }82

83

84 @TargetApi(Build.VERSION_CODES.N)85 private staticContext updateResources(Context context, String language) {86 Resources resources =context.getResources();87 Locale locale =AppLanguageUtils.getLocaleByLanguage(language);88

89 Configuration configuration =resources.getConfiguration();90 configuration.setLocale(locale);91 configuration.setLocales(newLocaleList(locale));92 returncontext.createConfigurationContext(configuration);93 }94

95 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值