Android获取语言及地区总结

##Android获取语言及地区总结

Android中获取的地区是语言地区,它是随着系统语言的改变而改变的

获取语言和地区分为两种:
获取系统语言和获取当前资源语言

###获取系统语言:


//Android 7.0以前
Locale locale = Locale.getDefault();
Log.d("------------"+locale.getLanguage() + "-" + locale.getCountry());


//Android 7.0以后
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleListCompat listCompat=ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
            for (int i = 0; i < listCompat.size(); i++) {
                AppLog.d(i + " ------1> " + listCompat.get(i).getLanguage() + "-" + listCompat.get(i).getCountry());
            }
        }

###获取当前资源配置的语言:

Locale locale = getResources().getConfiguration().locale;
Log.d("------------"+locale.getLanguage() + “-” + locale.getCountry());

但是,在 Android 7.0 上:getResources().getConfiguration().locale 被标记为 deprecated 了,所以初步适配后是:

Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    locale = getResources().getConfiguration().getLocales().get(0);
} else {
    locale = getResources().getConfiguration().locale;
}

//或者仅仅使用 locale = Locale.getDefault(); 不需要考虑接口 deprecated(弃用)问题
String lang = locale.getLanguage() + “-” + locale.getCountry();

###参考
https://likfe.com/2017/05/10/android-sys-language/
https://www.jishux.com/p/3e7b32b1d4d8693a

猜你喜欢

转载自blog.csdn.net/dreamsever/article/details/80973300
今日推荐