如何设置三种不同的TEXTSIZE在android的单一的TextView三种、如何设置、不同、TextView

2023-09-12 05:49:37 作者:无畏向前

我要设置单独的TextView三种不同的文字大小像下面的图片示例,

I want to set the three different text size in single textview as like the below image sample,

我曾尝试与HTML作为像下面,

I have tried with Html as like the below,

 StringBuilder fontString = new StringBuilder();

    fontString.append("<small>");
    fontString.append("1.78");
    fontString.append("</small>");      
    fontString.append("<big><b>");
    fontString.append("33");
    fontString.append("</b></big>");        
    fontString.append("<sup>");
    fontString.append("<small>");
    fontString .append("2");
    fontString.append("</small>");
    fontString.append("</sup>");

我也试过SpannableString格式,

Also I tried SpannableString formatting,

SpannableString  spanString = new SpannableString("1.34456");
    spanString.setSpan(new RelativeSizeSpan(1.75f), 0,spanString.length()-2-1, 0);
    spanString.setSpan(new StyleSpan(Typeface.BOLD), spanString.length()-2-1, spanString.length()-1, 0);
    spanString.setSpan(new ForegroundColorSpan(Color.RED), spanString.length()-2-1,  spanString.length()-1, 0);     
    spanString.setSpan(new RelativeSizeSpan(3f), spanString.length()-2-1, spanString.length()-1, 0);        
    spanString.setSpan(new RelativeSizeSpan(1.25f), spanString.length()-1, spanString.length(), 0);

像下面提到图像,

like the below mentioned image,

请给我建议,如果你知道解决的办法,我曾尝试与标或下标也,但没有运气。

Please suggest me if you know the solution,I have tried with superscript or subscript also but no luck.

推荐答案

请找到解决方案,

使用这个类来对齐文本,

Use this class to align the Text,

  import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;

public class SuperscriptSpanAdjuster extends MetricAffectingSpan {
    double ratio = 0.5;

    public SuperscriptSpanAdjuster() {
    }

    public SuperscriptSpanAdjuster(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);
    }

}

在这里,你可以找到SpannedString格式,

Here, you can find the SpannedString format,

SpannableString  spanString = new SpannableString(value);
    /* To show the text in top aligned(Normal)*/
    spanString.setSpan(new SuperscriptSpanAdjuster(0.7), 0,spanString.length()-mostSignificantLength-leastSignificantLength, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    /* Show the number of characters is normal size (Normal)*/
    spanString.setSpan(new RelativeSizeSpan(1.3f), 0,spanString.length()-mostSignificantLength-leastSignificantLength, 0);
    /*To set the text style as bold(MostSignificant)*/
    spanString.setSpan(new StyleSpan(Typeface.BOLD), spanString.length()-mostSignificantLength-leastSignificantLength, spanString.length()-leastSignificantLength, 0);
    /*To set the text color as WHITE(MostSignificant)*/
    spanString.setSpan(new ForegroundColorSpan(Color.WHITE), spanString.length()-mostSignificantLength-leastSignificantLength,  spanString.length()-leastSignificantLength, 0);
    /*Show the number of characters as most significant value(MostSignificant)*/
    spanString.setSpan(new RelativeSizeSpan(2.3f), spanString.length()-mostSignificantLength-leastSignificantLength, spanString.length()-leastSignificantLength, 0);

    /* To show the text in top aligned(LestSignificant)*/
    spanString.setSpan(new SuperscriptSpanAdjuster(1.2), spanString.length()-leastSignificantLength, spanString.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    /*To set the text style as bold(LestSignificant)*/
    spanString.setSpan(new StyleSpan(Typeface.BOLD), spanString.length()-leastSignificantLength, spanString.length(), 0);
    /*Show the number of characters as most significant value(LestSignificant)*/
    spanString.setSpan(new RelativeSizeSpan(0.8f), spanString.length()-leastSignificantLength, spanString.length(), 0);

快乐编码...

Happy coding...