Android针对TextView文本框内容设置不同字体颜色

一时失言乱红尘 2022-05-17 11:09 565阅读 0赞

Android开发中,TextView文本内容有时候需要不同的文字需要显示不同的颜色,如下下:

会员158****6317已登录,退出。

70

那这如何实现呢?

Java的Android环境这样写:

  1. SpannableString spannableString = new SpannableString(phone);
  2. spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), 0, spannableString.length() - 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  3. spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#009DE2")), spannableString.length() - 2, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  4. tv_Inxex_MemberInfo.setText(spannableString);

在.Net的Xamarin的Android开发中参照下面写:

SpannableString spannableString = new SpannableString(phone);
spannableString.SetSpan(new ForegroundColorSpan(Application.Context.Resources.GetColor(Resource.Color.systemSmallGray)), 0, spannableString.Length() - 2, SpanTypes.ExclusiveExclusive);
spannableString.SetSpan(new ForegroundColorSpan(Color.ParseColor(“#009DE2”)), spannableString.Length() - 2, spannableString.Length(), SpanTypes.ExclusiveExclusive);
tv_Inxex_MemberInfo.SetText(spannableString, BufferType.Spannable);

发表评论

表情:
评论列表 (有 0 条评论,565人围观)

还没有评论,来说两句吧...

相关阅读