public class MyLineBackgroundSpan
implements LineBackgroundSpan {
private int backgroundColor = 0;
private int padding = 0;
public MyLineBackgroundSpan(int backgroundColor, int padding) {
this.backgroundColor = backgroundColor;
this.padding = padding;
}
@Override
public void drawBackground(Canvas c, Paint p, int left, int right, int top, int baseline, int bottom, CharSequence text, int start, int end, int lnum) {
final int textWidth = Math.round(p.measureText(text, start, end));
final int paintColor = p.getColor();
final Paint.FontMetrics fm = p.getFontMetrics();
// using fontMetrics is to free to lineSpacingExtra or includeFontPadding of TextView attributes.
RectF rect = new RectF(left - padding,
baseline + fm.ascent - padding,
left + textWidth + padding,
baseline + fm.descent + padding);
p.setColor(backgroundColor);
c.drawRect(rect, p);
p.setColor(paintColor);
}
}
樣品
see
int padding = 10;
SpannableString spannable = new SpnnableString("...");
spannable.setSpan(
new MyLineBackgroundSpan(ContextCompat.getColor(context, R.color.colorPrimaryDark), padding),
0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setShadowLayer(padding, 0, 0, 0);
textView.setPadding(padding, padding, padding, padding);
textView.setText(spannable);
使用'android.text.style.LineBackgroundSpan' – pskink
檢查此[SO鏈接](http://stackoverflow.com/a/ 10676390/3831557)執行 –
嗨pskink謝謝你的評論你可以詳細說明這個概念作爲這個查詢的答案嗎? – Christ