android -语言设置

来源:互联网 发布:云软件哪个好 编辑:程序博客网 时间:2024/04/29 15:11

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

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

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

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

   :values-zh下string.xml  中<stringname="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();    }

原创粉丝点击