我有一个带有一些点的字符串,我从存储模型得到的,我想加粗并为我从数据存储模型得到的点字符串着色。我如何做到这一点,我尝试了下面提到的代码。
//Making a part of text from terms and condition spannable
SpannableStringBuilder sb = new SpannableStringBuilder("App " + CWalletDataModel.getInstance().getS_szWalletBalance() + getString(R.string.points_text));
// Span to set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(RewardUtil.getColor(mContext,R.color.colorAccent));
// create a bold StyleSpan to be used on the SpannableStringBuilder
StyleSpan b = new StyleSpan(Typeface.BOLD); // Span to make text bold
// Set the text color for first 4 characters
sb.setSpan(fcs, 8, 8 + CWalletDataModel.getInstance().getS_szWalletBalance().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// set only the name part of the SpannableStringBuilder to be bold --> 16, 16 + name.length()
sb.setSpan(b, 8, 8 + CWalletDataModel.getInstance().getS_szWalletBalance().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
HomeScreenActivity.m_toolbarTitle.setText(sb);
这里我只想粗体“CWalletDataModel.getInstance()。getS_szWalletBalance()”。
答案 0 :(得分:0)
那是因为App +空格是4个字符(“App”),而不是8.像在评论中写的那样在第4个位置设置5000的开头,然后在“App”之后开始spannable选项
sb.setSpan(fcs, 4, 4 + test.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(b, 4, 4 + test.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);