APP中英文切换

MainActivity代码:

 public static void changeLanguage(Context context, int lang) {
        String language = null;
        String country = null;
        switch (lang) {
            case 1:
                language = "zh";
                country = "CN";
                break;
            default:
                language = "en";
                country = "US";
                break;
        }

        Locale locale = new Locale(language, country);
        Configuration configuration = context.getResources().getConfiguration();
        configuration.locale = locale;
        context.getApplicationContext().getResources().updateConfiguration(configuration, context
                .getResources().getDisplayMetrics());
    }

Resource资源:
values文件夹下strings.xml代码:

<resources>
    <string name="app_name">hellostring>
resources>

res目录下新建文件夹values-zh-rCN,在新目录下,新建string.xml,代码如下:

<resources>
    <string name="app_name">哈哈string>
resources>

保证values目录与values-zh-rCN目录下string.xml里面属性值一一对应,调用上述方法,即可达到中英文切换问题。

你可能感兴趣的:(APP中英文切换)