首页 > 解决方案 > Not able to use HTML text in setText of collapsing Toolbar in android?

问题描述

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.

标签: androiduitextviewandroid-collapsingtoolbarlayouthtmltext

解决方案


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


推荐阅读