android -语言设置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23575795/article/details/78603428

在android中语言适配 响应国际化,一种场景是手动进行语言切换 ,另一种是根据定位得到国家码匹配。

 这里仅对中英文切换配置:

  1.在res文件下创建对应的values-en文件夹 和 values-zh文件夹 创建所属strings.xml;

   如: values-en下strings.xml  中<string name="hello_world">hello word</string>

   :values-zh下string.xml  中<string name="hello_world">您好,世界</string>

2.更新Configuration中的locale属性:

public static  void resetLanguage(int language){
//        int language = SPHelper.getInt(SpKeys.SP_KEY_LANGUAGE);

        //根据读取到的数据,进行设置
        Resources resources = App.get().getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        Configuration configuration = resources.getConfiguration();

        switch (language){
            case 0:
                configuration.setLocale( Locale.CHINESE);
                break;
            case 1:
                configuration.setLocale(Locale.ENGLISH);
                break;
            default:
                configuration.setLocale(Locale.getDefault());
                break;
        }

        resources.updateConfiguration(configuration,displayMetrics);
        // restart 重新启动首页
        App.reStart();
    }

猜你喜欢

转载自blog.csdn.net/qq_23575795/article/details/78603428
今日推荐