本文介绍了SpannableStringBuilder创建的字符串与多种字体/文本大小等例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个字符串放置在一个TextView,将显示一个像这样的字符串:

所以,我想知道我怎么可以使用<$c$c>SpannableStringBuilder要做到这一点?

我可以用三个文本编辑来完成这一点,但我想用最好的解决方案。

解决方案

 第一部分非粗体BOLD休息不够大胆
 

您可以做到这一点无论是作为@Rajesh建议或通过这一点。

 字符串normalBefore =第一部分不够大胆;
字符串normalBOLD =大胆;
字符串normalAfter =休息不够大胆;
字符串finalString = normalBefore + normalBOLD + normalAfter;
Spannable SB =新SpannableString(finalString);
sb.setSpan(新StyleSpan(android.graphics.Typeface.BOLD),finalString.indexOf(normalBOLD),normalBOLD.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //大胆
sb.setSpan(新AbsoluteSizeSpan(intSize),finalString.indexOf(normalBOLD),normalBOLD.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //调整大小
 

在TextView中显示此

  textview.setText(某人);
 

I need to create a String placed in a TextView that will display a string like this:

So I want to know how I could use SpannableStringBuilder to do this?

I could use three TextEdit to accomplish this but I would like to use best solution.

解决方案
First Part Not Bold   BOLD  rest not bold

You can do this either as @Rajesh suggested or by this.

String normalBefore= "First Part Not Bold ";
String normalBOLD=  "BOLD ";
String normalAfter= "rest not bold";
String finalString= normalBefore+normalBOLD+normalAfter;
Spannable sb = new SpannableString( finalString );
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), finalString.indexOf(normalBOLD), normalBOLD.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold
sb.setSpan(new AbsoluteSizeSpan(intSize), finalString.indexOf(normalBOLD), normalBOLD.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//resize size

to show this in TextView

textview.setText(sb);

这篇关于SpannableStringBuilder创建的字符串与多种字体/文本大小等例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:54