setSpan()只会生成一个图像

时间:2013-08-13 20:46:00

标签: android textview android-image spannablestring

            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()&#39; s看看最后一次迭代是否会填充2个图像。它只填充字符串最末端的图像。也许有一个标志我必须投入setpan来产生多个?

1 个答案:

答案 0 :(得分:0)

试试这个:

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++;
            }