Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.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
Android 安卓按钮的文本不是';t以指定的颜色显示_Android_Button_Text_Spannable - Fatal编程技术网

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");

给这只猫剥皮的几种方法-这里的答案很好:给这只猫剥皮的几种方法-这里的答案很好: