相关疑难解决方法(0)

如何制作我的自定义 ReplacementSpan 包装?

我正在尝试将带边框的背景应用于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)

android spannablestring

7
推荐指数
0
解决办法
1224
查看次数

标签 统计

android ×1

spannablestring ×1