为按钮文字使用2种(或更多)颜色 [英] Use 2 (or more) colors for a button text
本文介绍了为按钮文字使用2种(或更多)颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以通过以下方式更改按钮上文本的颜色:
I know I can change the color of the text on a button by the following ways :
button.setTextColor(getApplication().getResources().getColor(R.color.red)); //TAKE DEFAULT COLOR
或
button.setTextColor(0xff0000); //SET CUSTOM COLOR
或
button.setTextColor(Color.parseColor("#ff0000"));
但是我想为文本使用两种颜色:例如,假设我的按钮具有以下文本:"Click Here",我希望"Click"部分显示为红色,而"Here"部分显示为蓝色.
However I want to use two colors for the text : for example, suppose my button has the following text : "Click Here" , I want the "Click" part to appear in red, and the "Here" part in blue.
这可能吗?
推荐答案
您应使用 ForegroundColorSpan
尝试这样,
Button b = (Button) findViewById(R.id.button1);
SpannableString text = new SpannableString("Click Here");
// make "Clicks" (characters 0 to 5) Red
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);
// make "Here" (characters 6 to 10) Blue
text.setSpan(new ForegroundColorSpan(Color.BLUE), 6, 10, 0);
// shove our styled text into the Button
b.setText(text, BufferType.SPANNABLE);
输出:
希望这会对您有所帮助.
Hope this will help you.
这篇关于为按钮文字使用2种(或更多)颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文