如何在运行时在android中使部分文本加粗?
我的应用程序中的如何在运行时在android中使部分文本加粗?,android,android-listview,Android,Android Listview,我的应用程序中的列表视图有许多字符串元素,如名称,体验,加入日期,等等。我只想将名称加粗。所有字符串元素都将位于单个TextView中 我的XML: 假设您有一个名为etx的TextView。然后您将使用以下代码: final SpannableStringBuilder sb = new SpannableStringBuilder("HELLOO"); final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD)
列表视图
有许多字符串元素,如名称
,体验
,加入日期
,等等。我只想将名称
加粗。所有字符串元素都将位于单个TextView
中
我的XML:
假设您有一个名为
etx
的TextView
。然后您将使用以下代码:
final SpannableStringBuilder sb = new SpannableStringBuilder("HELLOO");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC); //Span to make text italic
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
sb.setSpan(iss, 4, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 2 characters Italic
etx.setText(sb);
我建议将strings.xml文件与CDATA一起使用
<string name="mystring"><![CDATA[ <b>Hello</b> <i>World</i> ]]></string>
根据Imran Rana的回答,如果您需要将
StyleSpan
s应用于多个TextView
s,并支持多种语言(其中索引是可变的),这里有一个通用的、可重用的方法:
在活动中使用它,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
StyleSpan boldStyle = new StyleSpan(Typeface.BOLD);
setTextWithSpan((TextView) findViewById(R.id.welcome_text),
getString(R.string.welcome_text),
getString(R.string.welcome_text_bold),
boldStyle);
// ...
}
strings.xml
<string name="welcome_text">Welcome to CompanyName</string>
<string name="welcome_text_bold">CompanyName</string>
欢迎使用公司名称
公司名称
结果:
欢迎使用CompanyName这里提供的答案是正确的,但不能在循环中调用,因为StyleSpan
对象是单个连续的span(不是可以应用于多个span的样式)。使用相同的粗体StyleSpan
多次调用setSpan
将创建一个粗体span,并在父span中移动它
在我的例子中(显示搜索结果),我需要使所有搜索关键字的所有实例都显示为粗体。这就是我所做的:
private static SpannableStringBuilder emboldenKeywords(final String text,
final String[] searchKeywords) {
// searching in the lower case text to make sure we catch all cases
final String loweredMasterText = text.toLowerCase(Locale.ENGLISH);
final SpannableStringBuilder span = new SpannableStringBuilder(text);
// for each keyword
for (final String keyword : searchKeywords) {
// lower the keyword to catch both lower and upper case chars
final String loweredKeyword = keyword.toLowerCase(Locale.ENGLISH);
// start at the beginning of the master text
int offset = 0;
int start;
final int len = keyword.length(); // let's calculate this outside the 'while'
while ((start = loweredMasterText.indexOf(loweredKeyword, offset)) >= 0) {
// make it bold
span.setSpan(new StyleSpan(Typeface.BOLD), start, start+len, SPAN_INCLUSIVE_INCLUSIVE);
// move your offset pointer
offset = start + len;
}
}
// put it in your TextView and smoke it!
return span;
}
请记住,如果一个关键字是另一个关键字的子字符串,那么上面的代码不足以跳过双黑体。例如,如果您在“Fishs in the fisty Sea”中搜索“Fish fi”,它将使“Fish”加粗一次,然后将“fi”部分加粗一次。好的一面是,虽然效率低下且有点不受欢迎,但它不会有视觉上的缺陷,因为显示的结果仍然是这样
Fishes在fisty Sea扩展frieder的答案,以支持案例和变音不敏感
public static String stripDiacritics(String s) {
s = Normalizer.normalize(s, Normalizer.Form.NFD);
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
return s;
}
public static void setTextWithSpan(TextView textView, String text, String spanText, StyleSpan style, boolean caseDiacriticsInsensitive) {
SpannableStringBuilder sb = new SpannableStringBuilder(text);
int start;
if (caseDiacriticsInsensitive) {
start = stripDiacritics(text).toLowerCase(Locale.US).indexOf(stripDiacritics(spanText).toLowerCase(Locale.US));
} else {
start = text.indexOf(spanText);
}
int end = start + spanText.length();
if (start > -1)
sb.setSpan(style, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
}
如果您不知道要加粗的文本部分之前的文本长度,或者甚至不知道要加粗的文本长度,您可以轻松使用如下HTML标记:
yourTextView.setText(Html.fromHtml("text before " + "<font><b>" + "text to be Bold" + "</b></font>" + " text after"));
yourTextView.setText(Html.fromHtml(“文本在“+”+”前面加上粗体“+”+”后面加上“+”文本”);
您可以使用Kotlin和buildSpandString
扩展函数从core ktx
holder.textView.text = buildSpannedString {
bold { append("$name\n") }
append("$experience $dateOfJoining")
}
如果使用@srings/your_string注释,请访问strings.xml文件,并在所需文本部分使用
标记
例如:
<string><b>Bold Text</b><i>italic</i>Normal Text</string>
粗体文本斜体标准文本
给定的名称不正确,请重试
在string.xml文件中使用“b”标记。
同样对于斜体“i”和下划线“u”。对于Xamarin,使用如下var bss=new StyleSpan(Android.Graphics.TypefaceStyle.Bold)
对于Xamarin,etx.textformated=sb嘿,伙计,你能看看这个吗
public static String stripDiacritics(String s) {
s = Normalizer.normalize(s, Normalizer.Form.NFD);
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
return s;
}
public static void setTextWithSpan(TextView textView, String text, String spanText, StyleSpan style, boolean caseDiacriticsInsensitive) {
SpannableStringBuilder sb = new SpannableStringBuilder(text);
int start;
if (caseDiacriticsInsensitive) {
start = stripDiacritics(text).toLowerCase(Locale.US).indexOf(stripDiacritics(spanText).toLowerCase(Locale.US));
} else {
start = text.indexOf(spanText);
}
int end = start + spanText.length();
if (start > -1)
sb.setSpan(style, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
}
yourTextView.setText(Html.fromHtml("text before " + "<font><b>" + "text to be Bold" + "</b></font>" + " text after"));
holder.textView.text = buildSpannedString {
bold { append("$name\n") }
append("$experience $dateOfJoining")
}
<string><b>Bold Text</b><i>italic</i>Normal Text</string>
<string name="My_Name">Given name is <b>Not Right</b>Please try again </string>