How to make RelativeSizeSpan align to top

后端 未结 9 2004
情深已故
情深已故 2021-01-31 03:14

I have the following String RM123.456. I would like to

  • Make RM relatively smaller
  • Make RM aligned to to
相关标签:
9条回答
  • 2021-01-31 03:37

    the best suitable solution is go with html.

    i will prefer for this solutions,it supports in all android version as well devices.

    here is example take it same as you want text

    <p><sup>TM</sup> 123.456.</p>
    

    i am getting result in android

    TM 123.456.

    you can easily display text in Textview in android with

    Html.fromText("YOUR_STRING_INHTML");

    Hope it helps.

    0 讨论(0)
  • 2021-01-31 03:39

    I know this is an old thread, but I just came across this today, and I'm surprised no one answered with this... I'd recommend not hardcoding the 2, 4 but you can figure out how to

    val span = SpannableStringBuilder.valueOf("$temp")
    span.append("°F", SuperscriptSpan(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    span.setSpan(RelativeSizeSpan(0.5f), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    tempTextView.setText(span, TextView.BufferType.SPANNABLE)
    
    0 讨论(0)
  • 2021-01-31 03:40

    Class for top align which should be used instead of RelativeSizeSpan (not in additional to):

    import android.text.TextPaint;
    import android.text.style.MetricAffectingSpan;
    
    public class TopRelativeSizeSpan extends MetricAffectingSpan {
    
        private final float mProportion;
    
        public TopRelativeSizeSpan(float proportion) {
            mProportion = proportion;
        }
    
        @Override
        public void updateDrawState(TextPaint ds) {
            ds.baselineShift += (mProportion - 1) * (ds.getTextSize() - ds.descent());
            ds.setTextSize(ds.getTextSize() * mProportion);
        }
    
        @Override
        public void updateMeasureState(TextPaint ds) {
            updateDrawState(ds);
        }
    }
    

    And usage:

    spannableString.setSpan(new TopRelativeSizeSpan(0.50f), 0, index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString, TextView.BufferType.SPANNABLE);
    
    0 讨论(0)
提交回复
热议问题