setSpan() создаст только одно изображение

            SpannableStringBuilder authorText = new SpannableStringBuilder("");
            ImageSpan is = new ImageSpan(getActivity(), R.drawable.ic_birdhead);
            for (Author a : mStory.authors) {
                if (!TextUtils.isEmpty(a.authorName)) {

                String prefix = "";
                if (count == 0) {
                    prefix = "By: ";
                } else if (count > 0 && count < mStory.authors.size()-1) {
                    prefix = ", ";
                } else {
                    prefix = " and ";
                }

                authorText.append(prefix + a.authorName + " ");
                authorText.setSpan(is, authorText.length()-1, authorText.length(), 0);
                //authorText.setSpan(is, authorText.length()-2, authorText.length()-1, 0);
                              //^ I put a second one there just to check is two will populate
                count++;
            }

Итак... В любом случае, он проходит цикл for, но я поставил 2 setSpan(), чтобы посмотреть, будет ли последняя итерация заполнять 2 изображения. Он заполняет изображение только в самом конце строки. Может быть, есть определенный флаг, который я должен поставить для setpan, чтобы произвести несколько?

1 ответ

Попробуй это:

SpannableStringBuilder authorText = new SpannableStringBuilder("");
            ImageSpan is = new ImageSpan(getActivity(), R.drawable.ic_birdhead);
            for (Author a : mStory.authors) {
                if (!TextUtils.isEmpty(a.authorName)) {

                String prefix = "";
                if (count == 0) {
                    prefix = "By: ";
                } else if (count > 0 && count < mStory.authors.size()-1) {
                    prefix = ", ";
                } else {
                    prefix = " and ";
                }

                authorText.append(prefix + a.authorName + " ");
                authorText.setSpan(is, authorText.length()-1, authorText.length(), 0);

                is = new ImageSpan(getActivity(), R.drawable.ic_birdhead);

                authorText.setSpan(is, authorText.length()-2, authorText.length()-1, 0);
                              //^ I put a second one there just to check is two will populate
                count++;
            }
Другие вопросы по тегам