android 如何在单个文本视图中设置三个不同的文本大小

How to set three different textsize in single textview in android

我想在单个文本视图中设置三种不同的文本大小,如下图示例,

我试过 Html 如下所示,

 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 格式,

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);

喜欢下面提到的图片,

如果你知道解决办法请给我建议,我也试过上标或下标但没有成功。

您可以使用 Html 标签,例如

<h1>...<h6>

使用下面的代码

txtview.setText(Html.fromHtml("your text here with HTML heading tags");

希望对您有所帮助

您可以使用 SpannableString with RelativeSizeSpan 在一个文本视图中创建多种文本大小。将文本的重力设置为顶部,这样文本将附加到顶部边缘。

请找出解决方案,

使用此 class 对齐文本,

  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 格式,

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);

编码愉快...

你用的方法Html.fromhtml("your html formatted text")对我有用,谢谢。