How to make RelativeSizeSpan align to top

后端 未结 9 2018
情深已故
情深已故 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:31

    You could achieve top gravity by creating a custom MetricAffectingSpan class

    here is the code of custom class:

    public class CustomCharacterSpan extends MetricAffectingSpan {
        double ratio = 0.5;
    
        public CustomCharacterSpan() {
        }
    
        public CustomCharacterSpan(double ratio) {
            this.ratio = ratio;
        }
    
        @Override
        public void updateDrawState(TextPaint paint) {
            paint.baselineShift += (int) (paint.ascent() * ratio);
        }
    
        @Override
        public void updateMeasureState(TextPaint paint) {
            paint.baselineShift += (int) (paint.ascent() * ratio);
        }
    }
    

    Applying the span:

    spannableString.setSpan(new RelativeSizeSpan(0.50f), 0, index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableString.setSpan(new CustomCharacterSpan(), 0, index,
                    SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString, TextView.BufferType.SPANNABLE);
    

    Output:

    For more info on MetricAffectingSpan : http://developer.android.com/reference/android/text/style/MetricAffectingSpan.html

    Custom MetricAffectingSpan logic referred from : Two different styles in a single textview with different gravity and hieght

提交回复
热议问题