TextView 中是否可以有多种样式? [英] Is it possible to have multiple styles inside a TextView?

查看:23
本文介绍了TextView 中是否可以有多种样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为 TextView 中的不同文本段设置多种样式?

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

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

是否可以为每个文本元素设置不同的样式?例如,line1 粗体、word1 斜体等

开发人员指南的常见任务以及如何在 Android 中执行这些任务 包括 选择、突出显示或设置文本的样式部分:

<块引用>

//获取我们的 EditText 对象.EditText vw = (EditText)findViewById(R.id.text);//设置 EditText 的文本.vw.setText("斜体、高亮、粗体.");//如果这只是一个 TextView,我们可以这样做://vw.setText("斜体、高亮、粗体.", TextView.BufferType.SPANNABLE);//强制它使用 Spannable 存储,以便可以附加样式.//或者我们可以在 XML 中指定.//获取EditText的内部文本存储Spannable str = vw.getText();//创建我们的跨度部分,并为每个部分分配一个格式.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);

但这在文本中使用了明确的位置编号.有没有更干净的方法来做到这一点?

解决方案

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

mBox = new TextView(context);mBox.setText(Html.fromHtml("<b>" + title + "</b>" + "<br/>" +<小>"+ 描述 + "</small>"+ "<br/>"+<小>"+ 添加日期 + "</small>"));

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

Is it possible to set multiple styles for different pieces of text inside a TextView?

For instance, I am setting the text as follows:

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

Is it possible to have a different style for each text element? E.g., line1 bold, word1 italic, etc.

The developer guide's Common Tasks and How to Do Them in Android includes Selecting, Highlighting, or Styling Portions of Text:

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

But that uses explicit position numbers inside the text. Is there a cleaner way to do this?

解决方案

In case, anyone is wondering how to do this, here's one way: (Thanks to Mark again!)

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

For an unofficial list of tags supported by this method, refer to this link or this question: Which HTML tags are supported by Android TextView?

这篇关于TextView 中是否可以有多种样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆