Android将一行文本设置为多种样式时,可以使用 SpannableString 来实现
private void setTips(){ String big = "大字深色"; String small = "小字淡色"; Spannable tips = new SpannableString(big + small); tips.setSpan(new AbsoluteSizeSpan(16, true), 0, big.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tips.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.black)), 0, big.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tips.setSpan(new AbsoluteSizeSpan(14, true), big.length(), tips.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tips.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.gray)), big.length(), tips.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tipsTv.setText(tips); }
另外也可以使用html样式字符串,然后调用 Html.fromHtml(String source) 生成多样式的文本