所以我能够显示使用显示按钮的两种不同颜色
String redString= "<font color='#ee0000'>yay</font>";
String blackString = "<font color='#000000'>eureka</font>";
textView.setText(Html.fromHtml(redString + blackString));
的TextView与不同颜色的文字,但试图做同样的用
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(抱歉,我不能显示图像,由于低信誉)
我使用AS 2.3.3的Windows 10。模拟器使用带有API 25的Android 7。感谢您的帮助!
您可以在此线性布局上使用简单的线性布局(水平)和文本视图作为按钮和setOnClickListener。 –
检查SpannableString –
可能的解决方案是[这里](https://stackoverflow.com/questions/19500350/use-2-or-more-colors-for-a-button-text)。最好的问候 –