s.setSpan(new BackgroundcolorSpan(getResources().getcolor(R.color.selection_blue)),prevIndex,index,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
但是我也需要为这个文本设置填充.可能吗?
解决方法 您可以使用ReplacementSpan.在您的活动中:
TextVIEw TagsTextVIEw = (TextVIEw) mVIEw.findVIEwByID(R.ID.TagsTextVIEw);SpannableStringBuilder stringBuilder = new SpannableStringBuilder();SpannableString tag = new SpannableString("TEST");stringBuilder.append(tag);stringBuilder.setSpan(new Tagspan(getResources().getcolor(R.color.blue),getResources().getcolor(R.color.white)),stringBuilder.length() - tag.length(),stringBuilder.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);TagsTextVIEw.setText(stringBuilder,TextVIEw.BufferType.SPANNABLE);
Tagspan.java
public class Tagspan extends ReplacementSpan { private static final float padding = 50.0f; private RectF mRect; private int mBackgroundcolor; private int mForegroundcolor; public Tagspan(int backgroundcolor,int foregroundcolor) { this.mRect = new RectF(); this.mBackgroundcolor = backgroundcolor; this.mForegroundcolor = foregroundcolor; } @OverrIDe public voID draw(Canvas canvas,CharSequence text,int start,int end,float x,int top,int y,int bottom,Paint paint) { // Background mRect.set(x,top,x + paint.measureText(text,start,end) + padding,bottom); paint.setcolor(mBackgroundcolor); canvas.drawRect(mRect,paint); // Text paint.setcolor(mForegroundcolor); int xPos = Math.round(x + (padding / 2)); int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ; canvas.drawText(text,end,xPos,yPos,paint); } @OverrIDe public int getSize(Paint paint,FontMetricsInt fm) { return Math.round(paint.measureText(text,end) + padding); }}总结
以上是内存溢出为你收集整理的Spannable的Android填充?全部内容,希望文章能够帮你解决Spannable的Android填充?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)