【发布时间】:2020-09-02 17:25:06
【问题描述】:
在我的TextViews 之一中,我想将一些文本设置为这样的粗体:
为此,我使用以下内容:
final SpannableStringBuilder sb = new SpannableStringBuilder(itemView.getContext().getResources().getString( R.string.ActivityIShared_with, sharedTo.get( getAdapterPosition() ) ));
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
sb.setSpan(bss, 0, 11, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
tv_SharedWith.setText(sb);
在哪里
<string name="ActivityIShared_with">Shared with: %1$s</string>
如您所见,此代码有效。但是,我需要手动计算 R.string.ActivityISharedwith 的长度,直到 %1$s 的部分为 11 才能使其加粗。
也许几天后,我会改变那句话,我想避免每次都计算它的长度。
如果没有 sharedTo.get( getAdapterPosition() ) 的部分,我怎样才能得到它的长度?没有变量的字符串。
谢谢
【问题讨论】:
-
你试过正则表达式吗? ` String mydata = "String with: 1s$s";模式 pattern = Pattern.compile("^[A-Za-z, ]+:"); Matcher matcher = pattern.matcher(mydata);` 像这样的东西,你需要将模式作为静态变量,因为每次编译都会消耗很多