我想设置三种不同的文字大小在单一的TextView像下面的图像样本,
我已经使用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与RelativeSizeSpan在一个TextView中创建多个文本大小。 设置文本的重力顶部,文本将连接到顶部边缘。
请找到解决方案,
使用这个类来对齐文本,
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")
它为我工作,谢谢。