如何在运行时在android中使部分文本加粗? [英] How to make part of the text Bold in android at runtime?
本文介绍了如何在运行时在android中使部分文本加粗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序中的 ListView
有许多字符串元素,例如 name
、experience
、加入日期
等. 我只是想让 name
加粗.所有字符串元素都将在一个 TextView
中.
A ListView
in my application has many string elements like name
, experience
, date of joining
, etc. I just want to make name
bold. All the string elements will be in a single 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>
我设置ListView项的TextView的代码:
My code to set the TextView of the ListView item:
holder.text.setText(name + "
" + expirience + " " + dateOfJoininf);
推荐答案
假设您有一个名为 etx
的 TextView
.然后,您将使用以下代码:
Let's say you have a TextView
called etx
. You would then use the following code:
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);
这篇关于如何在运行时在android中使部分文本加粗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文