当前位置:   article > 正文

Android-语言设置流程分析_android localepicker getallassetlocales

android localepicker getallassetlocales
    Android手机语言切换行为,是通过设置-语言和输入法-语言来改变手机的语言,其实这个功能很少被用户使用。
    以Android5.1工程源码为基础,从设置app入手来分析和学习语言切换的过程:
    一、语言设置界面:
    首先在设置app中找到语言设置这个Preference,目前设置中界面大多都是Fragment,先找到语言和输入法的PreferenceScreen,与其对应的Fragment是InputMethodAndLanguageSettings.java,在其onCreate()方法中,首先是增加语言设置的preference:     
addPreferencesFromResource(R.xml.language_settings);
找到language_settings.xml,可发现如下代码:    
  1. <PreferenceScreen
  2. android:key="phone_language"
  3. android:title="@string/phone_language"
  4. android:fragment="com.android.settings.LocalePicker"
  5. />

于是断定LocalePicker就是语言设置的Fragment,它是ListFragment的子类,继承于framework中LocalePicker,并实现了父类的一个接口,其回调方法是onLocaleSelected(),Locale中文含义大致是语言环境,所以可推测这是设置语言后的一个回调方法,不确定的话,可打断点测试一下。然而此类中并没有关于语言设置界面数据适配的太多逻辑, 只是通过父类的方法创建了一个view:    
  1. @Override
  2. public View onCreateView(
  3. LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  4. final View view = super.onCreateView(inflater, container, savedInstanceState);
  5. final ListView list = (ListView) view.findViewById(android.R.id.list);
  6. Utils.forcePrepareCustomPreferencesList(container, view, list, false);
  7. return view;
  8. }
   所以更多逻辑应该在framework中的LocalePicker.java中。既然是ListFragment,那就必须有Adapter,在此类中有构建了一个Adapter:
  1. /**
  2. * Constructs an Adapter object containing Locale information. Content is sorted by
  3. * {@link LocaleInfo#label}.
  4. */
  5. public static ArrayAdapter<LocaleInfo> constructAdapter(Context context) {
  6. return constructAdapter(context, R.layout.locale_picker_item, R.id.locale);
  7. }
  8. public static ArrayAdapter<LocaleInfo> constructAdapter(Context context,
  9. final int layoutId, final int fieldId) {
  10. boolean isInDeveloperMode = Settings.Global.getInt(context.getContentResolver(),
  11. Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
  12. //获取系统支持语言的信息
  13. final List<LocaleInfo> localeInfos = getAllAssetLocales(context, isInDeveloperMode);
  14. final LayoutInflater inflater =
  15. (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  16. return new ArrayAdapter<LocaleInfo>(context, layoutId, fieldId, localeInfos) {
  17. @Override
  18. public View getView(int position, View convertView, ViewGroup parent) {
  19. View view;
  20. TextView text;
  21. if (convertView == null) {
  22. view = inflater.inflate(layoutId, parent, false);
  23. text = (TextView) view.findViewById(fieldId);
  24. view.setTag(text);
  25. } else {
  26. view = convertView;
  27. text = (TextView) view.getTag();
  28. }
  29. LocaleInfo item = getItem(position);
  30. text.setText(item.toString());
  31. text.setTextLocale(item.getLocale());
  32. return view;
  33. }
  34. };
  35. }
而此方法通过getAllAssetLocales()方法获取系统支持语言的信息:
  1. public static List<LocaleInfo> getAllAssetLocales(Context context, boolean isInDeveloperMode) {
  2. final Resources resources = context.getResources();
  3. //获取系统所支持的语言
  4. final String[] locales = Resources.getSystem().getAssets().getLocales();
  5. List<String> localeList = new ArrayList<String>(locales.length);
  6. Collections.addAll(localeList, locales);
  7. // Don't show the pseudolocales unless we're in developer mode.
  8. if (!isInDeveloperMode) {
  9. localeList.remove("ar-XB");
  10. localeList.remove("en-XA");
  11. }
  12. Collections.sort(localeList);
  13. final String[] specialLocaleCodes = resources.getStringArray(R.array.special_locale_codes);
  14. final String[] specialLocaleNames = resources.getStringArray(R.array.special_locale_names);
  15. final ArrayList<LocaleInfo> localeInfos = new ArrayList<LocaleInfo>(localeList.size());
  16. for (String locale : localeList) {
  17. final Locale l = Locale.forLanguageTag(locale.replace('_', '-'));
  18. if (l == null || "und".equals(l.getLanguage())
  19. || l.getLanguage().isEmpty() || l.getCountry().isEmpty()) {
  20. continue;
  21. }
  22. if (localeInfos.isEmpty()) {
  23. if (DEBUG) {
  24. Log.v(TAG, "adding initial "+ toTitleCase(l.getDisplayLanguage(l)));
  25. }
  26. localeInfos.add(new LocaleInfo(toTitleCase(l.getDisplayLanguage(l)), l));
  27. } else {
  28. // check previous entry:
  29. // same lang and a country -> upgrade to full name and
  30. // insert ours with full name
  31. // diff lang -> insert ours with lang-only name
  32. final LocaleInfo previous = localeInfos.get(localeInfos.size() - 1);
  33. if (previous.locale.getLanguage().equals(l.getLanguage()) &&
  34. !previous.locale.getLanguage().equals("zz")) {
  35. if (DEBUG) {
  36. Log.v(TAG, "backing up and fixing " + previous.label + " to " +
  37. getDisplayName(previous.locale, specialLocaleCodes, specialLocaleNames));
  38. }
  39. previous.label = toTitleCase(getDisplayName(
  40. previous.locale, specialLocaleCodes, specialLocaleNames));
  41. if (DEBUG) {
  42. Log.v(TAG, " and adding "+ toTitleCase(
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号