我知道我可以通过以下方式更改按钮上文字的颜色:
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”部分显示为蓝色
这可能吗?
答案 0 :(得分:9)
您应该使用 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);
<强>输出强>:
希望这会对你有所帮助。
答案 1 :(得分:3)
是的,有可能
喜欢这个
Button btn = (Button) findViewById(R.id.btn);
btn.setText(Html.fromHtml("<font color='red'>Click</font>"
+ "<font color='blue'> Here</font>"));