我正在尝试将带边框的背景应用于TextView. ABackgroundColorSpan很好用,但它没有边框。为了获得边框,我改为使用 customReplacementSpan和 override draw。它看起来很适合短词/句子,但它无法换行。如何制作我的自定义ReplacementSpan包装?



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_replacement_span);
final Context context = this;
final TextView tv = (TextView) findViewById(R.id.tv);
Spannable span = Spannable.Factory.getInstance().newSpannable("Some long string that wraps");
//Works great, but no border:
//span.setSpan(new BackgroundColorSpan(Color.GREEN), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//Has border, but doesn't wrap:
span.setSpan(new BorderedSpan(context), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(span, TextView.BufferType.SPANNABLE);
}
public static class BorderedSpan extends ReplacementSpan {
final Paint mPaintBorder, mPaintBackground;
int mWidth;
Resources r;
int …Run Code Online (Sandbox Code Playgroud)