在按钮中设置2种不同的颜色

时间:2016-06-13 19:30:15

标签: java android android-button

我有一个简单的笑话应用程序。我有一些按钮用这段代码设置文本:

bt_barjokes.setText(getString(R.string.bar_jokes)+ " ("+barjokes.size()+")");

完美运作,它给出了使用过的Arraylist的正确标题和大小:Bar Jokes(55)

问题是我想更改(55)的颜色和字体大小:

("+barjokes.size()+")")

我在本论坛中尝试了几种建议的解决方案,但无法使其发挥作用。

2 个答案:

答案 0 :(得分:2)

您可以使用ForegroundColorSpan更改文字颜色,使用AbsoluteSizeSpanRelativeSizeSpan更改字体大小。

答案 1 :(得分:2)

您可以查看@xizzhu的答案,还可以使用SpannableStringHtml.fromHtml

setText(Html.fromHtml("<font color=#343434>" +"<small>"+ "Little Black Text" +"</small>"+
"</font>"+"&nbsp&nbsp"+"<font color=#123456>" +"<big>" +"Big Blue Text" +"</big>"+ "</font>"));

@Edit :如果您想在按钮中使用此功能,请在xml中添加以下行:android:textAllCaps="false"

您可以使用:<small><big>,在Toolbar中进行测试:

enter image description here

并在Button中,它看起来像:

enter image description here

相关问题