Android Spannablecontent With Rounded Corners

前端 未结 8 871
无人及你
无人及你 2020-12-13 15:06

I am trying to change my string to make a badge with a number in the middle by using Spannable String. I can highlight the appropriate letter/number by setting the BackGrou

8条回答
  •  执念已碎
    2020-12-13 15:41

    Hopefully this answer simplifies it for those still looking...

    You can simply use a "chip" drawable. It does all the calculations correctly and is of much more minimal code.

    See Standalone ChipDrawable

    For completeness copied here:

    res/xml/standalone_chip.xml:

    
    

    in java:

    // Inflate from resources.
    ChipDrawable chip = ChipDrawable.createFromResource(getContext(), R.xml.standalone_chip);
    
    // Use it as a Drawable however you want.
    chip.setBounds(0, 0, chip.getIntrinsicWidth(), chip.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(chip);
    
    Editable text = editText.getText();
    text.setSpan(span, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    

    End result:

提交回复
热议问题