如何在android中的一个按钮中放入2个不同的文本?

时间:2013-06-18 10:05:27

标签: android xml button text

所以这张图片基本上显示了我想要做的事情: http://oi44.tinypic.com/24do2te.jpg

我想在已经有文字的按钮上添加静态文本(橙色A)。但我不知道怎么做。

2 个答案:

答案 0 :(得分:0)

您可以尝试Html.fromHTML(htmlString)。类似的东西:

myTextButton.setText(Html.fromHtml("<font color='#FFA500'>A: </font> whatever text you need here"));

答案 1 :(得分:0)

转到@blackbelt

Spannable wordtoSpan = new SpannableString("A:  Text");        
wordtoSpan.setSpan(new ForegroundColorSpan(Color.ORANGE), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
button.setText(wordtoSpan);

其中button是您的Button对象..

,其中

.setSpan(what,start,end,flag);

,其中

  1. 什么 有ForegroundColorSpan或StyleSpan或UnderlineSpan对象。 ForegroundColorSpan为color选择一个整数参数,StyleSpan也为style设置一个参数,但UnderlineSpan不接受任何参数。

  2. start 有字符串的起点,您可以在哪里开始样式颜色的字符串索引。

  3. 结束 有字符串的结束点,你会在哪里结束样式颜色的字符串索引。

  4. 标志 可以有1或0。

  5. 您还可以将字体大小更改为字符串的一部分.. refer this link for ref