如何在android的文本视图中更改文本大小?

问题描述 投票:1回答:2

我内部有textview,我想更改文本中间的文本大小,如下所示

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS81c2w1cS5wbmcifQ==” alt =“在此处输入图像描述”>

android textview
2个回答
2
投票

//使用Html.fromHtml(str)

String str="<font size =\"20\"><B>Bold</B> <br/> Then Normal Text<br/>
                         <i>Then Italic</i> </font>" +
                       "<br/> <font color=\"green\" >this is simple sentence </font>" +
                       "<br/> <font face=\"verdana\" >this is simple sentence </font>"+
                       "<br/><br/><br/><br/><a>this is simple sentence</a>";
      Spanned strHtml= Html.fromHtml(str);

       TextView tv = (TextView)findViewById(R.id.textView);
       tv.setText(strHtml);

或您可以使用

SpannableString


1
投票
String text1 = "Hi";
String text2 = "there";

SpannableString span1 = new SpannableString(text1);
span1.setSpan(new AbsoluteSizeSpan(textSize1), 0, text1.length(), SPAN_INCLUSIVE_INCLUSIVE);

SpannableString span2 = new SpannableString(text2);
span2.setSpan(new AbsoluteSizeSpan(textSize2), 0, text2.length(), SPAN_INCLUSIVE_INCLUSIVE);

// let's put both spans together with a separator and all
CharSequence finalText = TextUtils.concat(span1, " ", span2);
© www.soinside.com 2019 - 2024. All rights reserved.