添加权限
1 2 |
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> |
主要 API
获取系统中全部的 Locale
1 2 3 4 |
final HashSet<String> langTagsToIgnore = new HashSet<>(); private Set<LocaleStore.LocaleInfo> mLocaleList; mLocaleList = LocaleStore.getLevelLocales(context,langTagsToIgnore, null, false); |
获取当前已有的 Locals
1 |
final LocaleList localeList = LocalePicker.getLocales(); |
更新 Locals 数组, 实现切换语言
1 |
LocalePicker.updateLocales(mLocalesToSetNext); |
例子: 切换为中文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
//private Locale localeZN = new Locale("zh", "CN"); private Locale localeZN = new Locale.Builder().setLanguage("zh").setScript("Hans").setRegion("CN").build(); // 设置中文为第一选择 changeLanguage(Locale.CHINA); private void changeLanguage(Locale locale) { LocaleList localeList = LocalePicker.getLocales(); int index = -1; for (int i = 0; i < localeList.size(); i++) { Locale lc = localeList.get(i); if (locale.equals(lc)) { index = i; break; } } Locale[] localeArrayNew = new Locale[index < 0 ? localeList.size() + 1 : localeList.size()]; if (index < 0) { localeArrayNew[0] = locale; for (int i = 0; i < localeList.size(); i++) { localeArrayNew[i + 1] = localeList.get(i); } } else { for (int i = 0; i < localeList.size(); i++) { localeArrayNew[i] = localeList.get(i); } localeArrayNew[index] = localeArrayNew[0]; localeArrayNew[0] = locale; } LocaleList mLocalesToSetNext = new LocaleList(localeArrayNew); LocalePicker.updateLocales(mLocalesToSetNext); } |
mxguo.com
0 Comments