是否有可能为一个TextView内的不同文本块设置多个样式?

例如,我将文本设置如下:

tv.setText(line1 + "\n" + line2 + "\n" + word1 + "\t" + word2 + "\t" + word3);

是否可以为每个文本元素设置不同的样式?例如,行1加粗,字1斜体等。

开发者指南的常见任务和如何在Android中执行这些任务包括选择,突出显示或样式化文本部分:

// Get our EditText object. EditText vw = (EditText)findViewById(R.id.text); // Set the EditText's text. vw.setText("Italic, highlighted, bold."); // If this were just a TextView, we could do: // vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE); // to force it to use Spannable storage so styles can be attached. // Or we could specify that in the XML. // Get the EditText's internal text storage Spannable str = vw.getText(); // Create our span sections, and assign a format to each. str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

但这使用了文本中的显式位置编号。有更干净的方法吗?


当前回答

尝试HTML . fromhtml(),并用粗体和斜体HTML标记你的文本,例如:

Spanned text = Html.fromHtml("This mixes <b>bold</b> and <i>italic</i> stuff");
textView.setText(text);

其他回答

正如Jon所说,对我来说,这是最好的解决方案,你不需要在运行时设置任何文本,只使用这个自定义类HtmlTextView

public class HtmlTextView extends TextView {

  public HtmlTextView(Context context) {
      super(context);
  }

  public HtmlTextView(Context context, AttributeSet attrs) {
      super(context, attrs);
  }

  public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr) 
  {
      super(context, attrs, defStyleAttr);
  }

  @TargetApi(21)
  public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
      super(context, attrs, defStyleAttr, defStyleRes);
  }

  @Override
  public void setText(CharSequence s,BufferType b){
      super.setText(Html.fromHtml(s.toString()),b);
  }

}

就是这样,现在只把它放在XML中

<com.fitc.views.HtmlTextView
    android:id="@+id/html_TV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/example_html" />

用你的Html字符串

<string name="example_html">
<![CDATA[
<b>Author:</b> Mr Donuthead<br/>
<b>Contact:</b> me@donut.com<br/>
<i>Donuts for life </i>
]]>

尝试HTML . fromhtml(),并用粗体和斜体HTML标记你的文本,例如:

Spanned text = Html.fromHtml("This mixes <b>bold</b> and <i>italic</i> stuff");
textView.setText(text);

如果有人想知道如何做到这一点,这里有一种方法:(再次感谢马克!)

mBox = new TextView(context);
mBox.setText(Html.fromHtml("<b>" + title + "</b>" +  "<br />" + 
            "<small>" + description + "</small>" + "<br />" + 
            "<small>" + DateAdded + "</small>"));

关于这个方法支持的非官方标签列表,请参考这个链接或这个问题:Android TextView支持哪些HTML标签?

现在<b>元素已弃用。<strong>呈现为<b>, <em>呈现为<i>。

tv.setText(Html.fromHtml("<strong>bold</strong> and <em>italic</em> "));

这对我来说很好

支持的标签列表如下:

如果使用字符串资源,则可以添加一些简单的样式,例如使用HTML标记的粗体或斜体。目前支持的标签有:B(粗体)、I(斜体)、U(下划线)、TT(单行)、BIG、SMALL、SUP(上标)、SUB(下标)和STRIKE(划线)。例如,在res/values/strings.xml中,你可以这样声明: <资源> <string id="@+id/styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string> > < /资源

(来自http://developer.android.com/guide/faq/commontasks.html#selectingtext - Web Archive链接,<资源>错别字是在原始!)

它还表明在简单的情况下并不真正需要Html.fromHtml。