我正在使用collapsingToolbar,我想添加HTML文本,但是我无法做到这一点。
下面我已经实现了,但是没有用。
private void initCollapsingToolbar() {
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.setExpanded(true);
//now that you have access to your toolbar, set the directions.
collapsingToolbar.setExpandedTitleGravity(Gravity.CENTER_HORIZONTAL);
collapsingToolbar.setCollapsedTitleGravity(Gravity.RIGHT);
String part1 = " asdasd ";
String part2 = "Hi";
SpannableString str = new SpannableString(part1 + part2);
str.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0000")), part1.length(), str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
collapsingToolbar.setTitle(str);
// hiding & showing the title when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
String part1 = " asdasd ";
String part2 = "Hi";
SpannableString str = new SpannableString(part1 + part2);
str.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0000")), part1.length(), str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
collapsingToolbar.setTitle(str);
isShow = true;
} else if (isShow) {
String part1 = " asdasd ";
String part2 = "Hi";
SpannableString str = new SpannableString(part1 + part2);
str.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0000")), part1.length(), str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
collapsingToolbar.setTitle(str);
isShow = false;
}
}
});
}
HTML无法在折叠的工具栏上实现