Hou*_*fly 80 android android-listview
一个ListView在我的应用程序有很多字符串元素,如name,experience,date of joining,等我只是想name大胆.所有字符串元素都在一个单独的元素中TextView.
我的XML:
<ImageView
android:id="@+id/logo"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/logo"
android:padding="5dp"
android:textSize="12dp" >
</TextView>
Run Code Online (Sandbox Code Playgroud)
我的代码设置ListView项的TextView:
holder.text.setText(name + "\n" + expirience + " " + dateOfJoininf);
Run Code Online (Sandbox Code Playgroud)
Imr*_*ana 206
说你有一个TextView叫etx.然后,您将使用以下代码:
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);
Run Code Online (Sandbox Code Playgroud)
fri*_*mle 23
根据Imran Rana的回答,如果您需要将StyleSpans应用于多个TextViews,并支持多种语言(其中索引是可变的),这里是一个通用的,可重用的方法:
void setTextWithSpan(TextView textView, String text, String spanText, StyleSpan style) {
SpannableStringBuilder sb = new SpannableStringBuilder(text);
int start = text.indexOf(spanText);
int end = start + spanText.length();
sb.setSpan(style, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
}
Run Code Online (Sandbox Code Playgroud)
Activity像这样使用它:
@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);
// ...
}
Run Code Online (Sandbox Code Playgroud)
strings.xml
<string name="welcome_text">Welcome to CompanyName</string>
<string name="welcome_text_bold">CompanyName</string>
Run Code Online (Sandbox Code Playgroud)
结果:
欢迎来到CompanyName
Dmi*_*nov 14
你可以使用 Kotlin 和buildSpannedString扩展函数来做到这一点core-ktx
holder.textView.text = buildSpannedString {
bold { append("$name\n") }
append("$experience $dateOfJoining")
}
Run Code Online (Sandbox Code Playgroud)
这里提供的答案是正确的,但不能在循环中调用,因为该StyleSpan对象是单个连续的跨度(不是可以应用于多个跨度的样式).setSpan使用相同的粗体调用多次StyleSpan将创建一个粗体跨度,并在父级跨度中移动它.
在我的情况下(显示搜索结果),我需要使所有搜索关键字的所有实例都显示为粗体.这就是我做的:
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;
}
Run Code Online (Sandbox Code Playgroud)
请记住,如果一个关键字是另一个关键字的子字符串,则上面的代码不够智能,不能跳过双粗体.例如,如果您在"狩猎海中的鱼"中搜索"Fish fi" ,它将使"鱼"大胆一次,然后是"fi"部分.好处是虽然效率低且有点不受欢迎,但它不会有视觉缺陷,因为显示的结果仍然会像
鱼在ES 网络猪圈海
如果您不确切知道要加粗的文本部分之前的文本长度,或者甚至不知道要加粗的文本长度,您可以轻松地使用 HTML 标记,如下所示:
yourTextView.setText(Html.fromHtml("text before " + "<font><b>" + "text to be Bold" + "</b></font>" + " text after"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53183 次 |
| 最近记录: |