安卓多语言切换时底部导航栏语言切换错误

本文介绍了一种解决安卓应用中多语言切换时底部导航栏语言显示不一致的问题的方法。通过在代码中设置字符串资源,确保底部导航栏的文字能正确地随着用户选择的语言变化而更新。此外,还详细介绍了如何根据不同安卓版本进行适配,包括使用`Configuration`对象来更改应用的默认语言环境。

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

安卓多语言切换时底部导航栏语言切换错误

切换的语言不一样,解决办法是在代码中设置string

String[] bottomString = getResources().getStringArray(R.array.bottom_navigation);
for (int i = 0; i < bottomString.length; i++) {
    mBottomNavigationView.getMenu().getItem(i).setTitle(bottomString[i]);
}

ok搞定

语言切换

Resources resources = App.getContext().getResources();
Configuration configuration = resources.getConfiguration();
Locale locale = isEn ? Locale.ENGLISH : Locale.CHINA;
configuration.setLocale(locale);
//安卓8.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    configuration.setLocales(new LocaleList( Locale.ENGLISH, Locale.CHINA));
    App.getContext().createConfigurationContext(configuration);
}
else {
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}

 

安卓8.0以上需要在activity里面的attachContext方法调用

Activity或application中重写这个方法

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(AppUtil.attachBaseContext(newBase));
}

AppUtil

public static Context attachBaseContext(Context context) {
    // 8.0需要使用createConfigurationContext处理
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return updateResources(context, getCurLanguage());
    } else {
        return context;
    }
}

@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, Locale locale) {
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(locale);
    configuration.setLocales(new LocaleList(locale));
    return context.createConfigurationContext(configuration);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值