【安卓学习之常见问题】 多国语言横竖屏时,自动切换到默认语言(android:configChanges的使用)

█ 问题重现:

 ● 手机系统语言设置为中文
 ● 启动app,app显示中文
 ● 在app中设置app的语言为其他国家语言,如英文:
   ● 界面:
 这里写图片描述
   ●  代码:

public static void updateLanguage(Context context, String languageCodeNew) {
        String languageCodeOld = getCurrentPhoneLanguage(context);
        Resources resources = context.getResources();// 获得res资源对象
        Configuration config = resources.getConfiguration();// 获得设置对象
        DisplayMetrics dm = resources.getDisplayMetrics();// 获得屏幕参数:主要是分辨率,像素等。
        if (!TextUtils.isEmpty(languageCodeNew)) {
            Locale locale = getLanguageLocale(languageCodeNew);
            if (locale != null)
                config.locale = locale;
            if (languageCodeOld.equals(config.locale.getLanguage())) {
                return;
            }
            Locale.setDefault(config.locale);
            resources.updateConfiguration(config, dm);
        }
    }
    public static String getCurrentPhoneLanguage(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        return language;
    }
    private static Locale getLanguageLocale(String languageCode) {
        if (!TextUtils.isEmpty(languageCode)) {
            if (languageCode.equals("0086")) {
                return Locale.CHINA; // 简体中文
            } else if (languageCode.equals("0001")) {
                return Locale.ENGLISH; // 英文
            } else if (languageCode.equals("0007")) {
                return new Locale("ru", "RU");
            } else if (languageCode.equals("0034")) {
                return new Locale("es", "ES");
            } 
            ......
        }
        return null;
    }

 ● 由于界面语言要重新加载,所以关闭所有Activity(界面),并跳转到登陆界面,重新进入子界面都显示为英文
 ● 出现的问题:当界面由竖屏改为横屏时,界面本来显示英文,结果一下子就自动切换为中文。
 

█ 问题原因:

 ● 在屏幕旋转时触发onConfigurationChanged(Configuration newConfig)方法时,这个newConfig取的是系统的,因此界面就显示为中文
  

█ 解决方法:

 ● 之前的解决方法:对于每个界面进行设置
 ○  在AndroidManifest.xml中Activity标签下添加android:configChanges=”orientation|screenSize|locale”,当横竖屏切换时,回调onConfigurationChanged方法

        <activity
            android:name="com.****.****.activity.LoginActivity"
            android:configChanges="orientation|screenSize|locale" />

 ○  在基类中实现重写onConfigurationChanged(Configuration newConfig)方法

abstract public class BaseActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {         
         ......
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        String languageCodeNew = ...// 要设置的新的语言编码
        updateLanguage(this, languageCodeNew);//代码内容见【问题重现】
    }
}

 
PS : 缺点:由于界每个界面都需要实现该功能,导致在AndroidManifest.xml中需要对每个activity进行android:configChanges的添加。由于是在查看AndroidManifest.xml文件时,感觉没用那么清晰!

 
 ● 新的解决方法:对于Application进行设置
 ○  今天我想看下看到了android:configChanges这部分是否可以通过代码来实现,很遗憾,没有找到相关文章,后来看到另外一篇文章,文章内容见【相关资料收集-1.Android 系统语言切换监听和设置实例代码】,于是进行了如下的修改,测试了一切都ok,代码也更简洁。
   
 ○  在AndroidManifest.xml中Activity标签下去掉原先的android:configChanges=”orientation|screenSize|locale”

        <activity android:name="com.****.****.activity.LoginActivity" />

 ○  去掉基类中的onConfigurationChanged(Configuration newConfig)方法(即使写了也没用,不被调用)
 ○ 在Application的实现类中,重写onConfigurationChanged(Configuration newConfig)方法

public class ******Application extends Application {
    @Override
    public void onCreate() {             
         ......
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        String languageCodeNew = ...// 要设置的新的语言编码
        updateLanguage(this, languageCodeNew);//代码内容见【问题重现】
    }
}

PS : 代码量节省了很多,看起来更清爽!由于是AndroidManifest.xml文件,基本上每个activity的声明,都只需要一行代码搞定!
 

█ 最终答案:

 ● 选择【解决方法】中的第二种方法,在Application中重写onConfigurationChanged方法,因为这个配置是整个应用设置的,在这里设置语言,能够改变整个应用的语言设置,而且这种方式,无需在AndroidManifest.xml中增加配置。
 ●【1.Android 系统语言切换监听和设置实例代码】资料中说,在Application中重写onConfigurationChanged方法后, Activity中的onConfigurationChanged依旧会被调用,不过我在测试的过程中,BaseActivity中的onConfigurationChanged方法没用被调用,因此后面我就直接去掉Activity中的onConfigurationChanged方法。

█ 相关资料收集:

1.Android 系统语言切换监听和设置实例代码

  一、代码中动态设置应用显示语言(手动控制使用values-zh-rCN下字符串)
  二、语言切换监听
  1.广播方式监听
  2.重写onConfigurationChanged方法
  

2.Android旋转屏幕后国际化语言失效的解决办法

转载请注明出处:
http://blog.csdn.net/ljb568838953/article/details/77005347

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值