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);
                //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 つの画像が取り込まれるかどうかを確認します。文字列の最後にのみ画像を取り込みます。たぶん、複数を生成するために setspan に入れなければならない特定のフラグがありますか?

4

1 に答える 1

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++;
            }
于 2013-08-13T21:21:53.903 に答える