如何使文字大胆的Andr​​oid在运行时的一部分吗?大胆、文字、oid、Andr

2023-09-04 08:25:54 作者:亡城孤影

A 的ListView 在我的应用程序有一个像名称经验,加入的日期等。我只想让名称大胆。所有字符串元素将在一个的TextView

我的XML:

 < ImageView的
    机器人:ID =@ + ID /标志
    机器人:layout_width =55dp
    机器人:layout_height =55dp
    机器人:layout_marginLeft =5DP
    机器人:layout_marginRight =5DP
    机器人:layout_marginTop =15dp>
< / ImageView的>

<的TextView
    机器人:ID =@ + /标记
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_toRightOf =@ ID /标志
    机器人:填充=5DP
    机器人:TEXTSIZE =12dp>
< / TextView的>
 

我的code设置ListView项的TextView的:

  holder.text.setText(姓名+\ N+经验性++ dateOfJoininf);
 

解决方案

假设你有一个的TextView 名为 ETX 。然后,您可以使用下面的code:

 最后SpannableStringBuilder SB =新SpannableStringBuilder(HELLOO);

最后StyleSpan BSS =新StyleSpan(android.graphics.Typeface.BOLD); //跨度,使文本加粗
最后StyleSpan ISS =新StyleSpan(android.graphics.Typeface.ITALIC); //跨度,使文本斜体
sb.setSpan(BSS,0,4,Spannable.SPAN_INCLUSIVE_INCLUSIVE); //使第4个字符粗体
sb.setSpan(ISS,4,6,Spannable.SPAN_INCLUSIVE_INCLUSIVE); //让最后2个字符斜体

etx.setText(某人);
 

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.

my 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>

My code to set the TextView of the ListView item:

holder.text.setText(name + "\n" + expirience + " " + dateOfJoininf);

解决方案

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