Android setSpan()将只生成一个图像 SpannableStringBuilder authorText=new SpannableStringBuilder(“”); ImageSpan is=新的ImageSpan(getActivity(),R.drawable.ic_birdhead); for(作者a:mStory.authors){ 如果(!TextUtils.isEmpty(a.authorName)){ 字符串前缀=”; 如果(计数=0){ prefix=“By:”; }else if(count>0&&count0&&count
所以..它无论如何都要经过一个for循环,但是我放了2个setSpan(),看看最后一次迭代是否会填充2个图像。它只会在字符串的最末端填充一个图像。也许我必须放一个特定的标志,以便setSpan生成多个图像?试试这个:Android setSpan()将只生成一个图像 SpannableStringBuilder authorText=new SpannableStringBuilder(“”); ImageSpan is=新的ImageSpan(getActivity(),R.drawable.ic_birdhead); for(作者a:mStory.authors){ 如果(!TextUtils.isEmpty(a.authorName)){ 字符串前缀=”; 如果(计数=0){ prefix=“By:”; }else if(count>0&&count0&&count,android,textview,android-image,spannablestring,Android,Textview,Android Image,Spannablestring,所以..它无论如何都要经过一个for循环,但是我放了2个setSpan(),看看最后一次迭代是否会填充2个图像。它只会在字符串的最末端填充一个图像。也许我必须放一个特定的标志,以便setSpan生成多个图像?试试这个: SpannableStringBuilder authorText = new SpannableStringBuilder(""); ImageSpan is = new ImageSpan(getActivity(), R.dr
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++;
}
SpannableStringBuilder authorText=new SpannableStringBuilder(“”);
ImageSpan is=新的ImageSpan(getActivity(),R.drawable.ic_birdhead);
for(作者a:mStory.authors){
如果(!TextUtils.isEmpty(a.authorName)){
字符串前缀=”;
如果(计数=0){
prefix=“By:”;
}else if(count>0&&count
您可能需要为每次迭代创建一个新的ImageSpan。您不能多次使用同一个span,它只会重新定位该span。刚刚解决了这个问题……谢谢!
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++;
}