【发布时间】:2010-11-08 16:01:29
【问题描述】:
我看到有一个TextAppearanceSpan 可用,但没有使用示例。我只想将文本加粗,并保持其他所有内容不变 - 是否有更简单的方法可以以编程方式执行此操作?
【问题讨论】:
-
仅供参考,这是用于在 Tab 小部件上设置标题。
标签: android text coding-style widget
我看到有一个TextAppearanceSpan 可用,但没有使用示例。我只想将文本加粗,并保持其他所有内容不变 - 是否有更简单的方法可以以编程方式执行此操作?
【问题讨论】:
标签: android text coding-style widget
您只需要在 res/values 中创建一个 xml 文件并编写如下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="header">
/** here goes the style */
</style>
</resources>
那么您只需将生成的 R.style.header 传递给 TextAppearanceSpan 构造函数。
【讨论】:
记录在
http://developer.android.com/guide/appendix/faq/commontasks.html#selectingtext
,看第二种方式使用Spannable。
具体
// Get our EditText object.
EditText vw = (EditText)findViewById(R.id.text); // or new etc
// Set the EditText's text.
vw.setText("Italic, highlighted, bold.");
// 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);
【讨论】: