Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在一个按钮中设置两种不同的颜色_Java_Android_Android Button - Fatal编程技术网

Java 在一个按钮中设置两种不同的颜色

Java 在一个按钮中设置两种不同的颜色,java,android,android-button,Java,Android,Android Button,我有一个带有笑话的简单应用程序。我使用以下代码设置文本的按钮: bt_barjokes.setText(getString(R.string.bar_jokes)+ " ("+barjokes.size()+")"); 工作完美,它给出了所用Arraylist的正确标题和大小:Bar笑话(55) 问题是我想更改(55)的颜色和字体大小: 我在这个论坛上尝试了几种建议的解决方案,但都没有成功。你可以使用ForegroundColorSpan更改文本颜色,使用AbsoluteSizeSpan或Re

我有一个带有笑话的简单应用程序。我使用以下代码设置文本的按钮:

bt_barjokes.setText(getString(R.string.bar_jokes)+ " ("+barjokes.size()+")");
工作完美,它给出了所用Arraylist的正确标题和大小:Bar笑话(55)

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


我在这个论坛上尝试了几种建议的解决方案,但都没有成功。

你可以使用
ForegroundColorSpan
更改文本颜色,使用
AbsoluteSizeSpan
RelativeSizeSpan
更改字体大小。

你可以看看@xizzhu的答案,您还可以使用
SpannableString
Html.fromHtml

setText(Html.fromHtml("<font color=#343434>" +"<small>"+ "Little Black Text" +"</small>"+
"</font>"+"&nbsp&nbsp"+"<font color=#123456>" +"<big>" +"Big Blue Text" +"</big>"+ "</font>"));
setText(Html.fromHtml(“+”+“小黑文本”+”)+
“+”+“+”+“+”蓝色大文本“+”);
@Edit:如果您想在按钮中使用此选项,请在xml中添加此行,如:
android:textAllCaps=“false”

您可以使用:
,在
工具栏中测试:

按钮中,它看起来像:


我尝试添加代码,但按钮文本不变。我尝试使用您的精确代码进行测试,但它只使用普通的黑色字体编写文本(小黑色文本大蓝色文本)。没有蓝色或更大的字体。@FlemmingWacherDanielsen我的朋友我已经为你编辑了我的答案,请查看编辑部分
setText(Html.fromHtml("<font color=#343434>" +"<small>"+ "Little Black Text" +"</small>"+
"</font>"+"&nbsp&nbsp"+"<font color=#123456>" +"<big>" +"Big Blue Text" +"</big>"+ "</font>"));