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

时间:2015-05-07 09:43:41

标签: android html android-layout android-intent android-activity

我想在单个textview中设置三种不同的文字大小,如下面的图片样本enter image description here

我已尝试使用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);

如下图所示, enter image description here

如果您知道解决方案,请建议我,我也尝试过上标或下标,但没有运气。

4 个答案:

答案 0 :(得分:0)

您可以使用Html标签,例如

<h1>...<h6>

使用以下代码

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

这可以帮助你

答案 1 :(得分:0)

您可以SpannableStringRelativeSizeSpan一起使用,在一个文本视图中创建多个文字大小。将文本的重力设置为TOP,以便将文本附加到顶部边缘。

答案 2 :(得分:0)

请找到解决方案,

使用此类对齐文本

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

快乐的编码......

答案 3 :(得分:0)

你使用方法x=randn(1000,1); 它对我有用,谢谢。