Android 安卓按钮的文本不是';t以指定的颜色显示
我有一个很简单的问题。我在活动中使用一个方法创建了几个按钮。但是,文本颜色仅保留标准颜色(灰色)。我在方法中定义了以下内容:Android 安卓按钮的文本不是';t以指定的颜色显示,android,button,text,spannable,Android,Button,Text,Spannable,我有一个很简单的问题。我在活动中使用一个方法创建了几个按钮。但是,文本颜色仅保留标准颜色(灰色)。我在方法中定义了以下内容: Button b = new Button(this); b.setTextColor(R.color.red); b.setText("Some text"); 有人知道这个问题并能帮我解决吗?通过谷歌搜索,我读到了一些关于Spanable的东西。但是,这似乎不适用于按钮文本。在告诉它您想要什么颜色之前,需要调用getResources() getResources(
Button b = new Button(this);
b.setTextColor(R.color.red);
b.setText("Some text");
有人知道这个问题并能帮我解决吗?通过谷歌搜索,我读到了一些关于Spanable的东西。但是,这似乎不适用于按钮文本。在告诉它您想要什么颜色之前,需要调用
getResources()
getResources().getColor(color)
您需要调用
getResources()
,然后才能告诉它您想要什么颜色
getResources().getColor(color)
R.color.red
是一个资源标识符(在Android中他们使用整数
)。您需要像这样使用该代码:
Resources res = getResources();
int red = res.getColor(R.color.red);
Button b = new Button(this);
b.setTextColor(red);
b.setText("Some text");
R.color.red
是一个资源标识符(在Android中他们使用整数
)。您需要像这样使用该代码:
Resources res = getResources();
int red = res.getColor(R.color.red);
Button b = new Button(this);
b.setTextColor(red);
b.setText("Some text");
给这只猫剥皮的几种方法-这里的答案很好:给这只猫剥皮的几种方法-这里的答案很好: