【发布时间】:2018-02-10 09:26:14
【问题描述】:
所以我可以使用不同的颜色显示 textView 的文本
String redString= "<font color='#ee0000'>yay</font>";
String blackString = "<font color='#000000'>eureka</font>";
textView.setText(Html.fromHtml(redString + blackString));
但是当试图做同样的事情时
button.setText(...)
它不起作用!你知道按钮的文本是否可以有不同的颜色吗?如果没有,是否有其他解决方案?
编辑:我尝试了建议的两种解决方案,但都没有奏效。为了确定,我创建了一个新项目:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//first solution
String redString= "<font color='#ee0000'>yay </font>";
String blackString = "<font color='#000000'>eureka</font>";
TextView test1 = (TextView) findViewById(R.id.textView);
Button test2 = (Button) findViewById(R.id.button2);
test1.setText(Html.fromHtml(redString + blackString));
test2.setText(Html.fromHtml(redString + blackString));
//second solution
Button b = (Button) findViewById(R.id.button);
SpannableString text = new SpannableString("Hello World");
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 4, 0);
text.setSpan(new ForegroundColorSpan(Color.BLUE), 5, text.length(), 0);
b.setText(text, TextView.BufferType.SPANNABLE);
}
}
And this is the result(抱歉,由于信誉低,无法显示图片)
我在 Windows 10 上使用 AS 2.3.3。模拟器使用带有 API 25 的 Android 7。感谢您的帮助!
【问题讨论】:
-
您可以使用简单的线性布局(水平),将文本视图作为按钮并在此线性布局上使用 setOnClickListener。
-
检查 SpannableString
-
可能的解决方案是here。最好的问候
-
请描述
... it doesn't work!。
标签: java android button text colors