0

I am using collapsing Toolbar layout in my android application. while layout is collapse or expand in both case I need collapsing Toolbar text displaying my "amount" in green and "balance" in red. I had try to use HTML like below:

collapsingToolbar.setTitle(Html.fromHtml("<font color='green'>50</font><font color='red'>balance</font>"));

I had used SpannableString like below:

String part1 = " 50";
String part2 = "Balance";
SpannableString str = new SpannableString(part1 + part2);
str.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0000")), part1.length(), str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
collapsingToolbar.setTitle(str);

Both did not work in my case. Dose someone have some new idea to work in this case.

4

2 回答 2

0

您应该在 Toolbar 小部件内添加一个 TextView 并将其文本设置为 HTML 格式的文本。

于 2018-07-20T11:05:24.053 回答
0

请使用这个

StringBuilder builder = new StringBuilder();
Spannable word1 = new SpannableString("50");
    word1.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green)), 0, word1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    collapsingToolbar.append(word1);
    Spannable word2 = new SpannableString("Balance");
    word2.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color. red)), 0, word2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.append(word2);
    collapsingToolbar.setTitle(builder);
于 2018-07-20T11:06:46.253 回答