首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

字体背景颜色的高度批改

2013-12-13 
字体背景颜色的高度修改当使用android:lineSpacingExtra5dp 后面背景就会变大public class BetterHighl

字体背景颜色的高度修改

字体背景颜色的高度批改

当使用android:lineSpacingExtra="5dp" 后面背景就会变大

public class BetterHighlightSpan extends ReplacementSpan {    private int backgroundColor;    public BetterHighlightSpan(int backgroundColor) {        super();        this.backgroundColor = backgroundColor;    }    @Override    public int getSize(Paint paint, CharSequence text, int start, int end, FontMetricsInt fm) {        return Math.round(paint.measureText(text, start, end));    }    @Override    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom,            Paint paint) {        // save current color        int oldColor = paint.getColor();        // calculate new bottom position considering the fontSpacing        float fontSpacing = paint.getFontSpacing();        float newBottom = bottom - fontSpacing;        // change color and draw background highlight        RectF rect = new RectF(x, top, x + paint.measureText(text, start, end), newBottom);        paint.setColor(backgroundColor);        canvas.drawRect(rect, paint);        // revert color and draw text        paint.setColor(oldColor);        canvas.drawText(text, start, end, x, y, paint);    }}TextView textView = (TextView) findViewById(R.id.textView);SpannableStringBuilder builder = new SpannableStringBuilder("here some text and more of it");builder.setSpan(new BetterHighlightSpan(Color.CYAN), 4, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);textView.setText(builder);

?

热点排行