在XML中,我们可以通过textColor属性设置文本颜色,比如android:textColor="#FF0000"。但是我如何通过编码来改变它呢?

我试过这样的方法:

holder.text.setTextColor(R.color.Red);

其中holder只是一个类,文本类型为TextView。红色是字符串中设置的RGB值(#FF0000)。

但是它显示的不是红色而是另一种颜色。我们可以在setTextColor()中传递什么样的参数?在文档中,它说的是int,但它是资源引用值还是其他什么?


当前回答

试试这个:

TextView textview = (TextView) findViewById(R.id.textview );
textview .setTextColor(Color.parseColor("#85F85F"));

其他回答

getColor()被禁用

所以试试这个方法:

 tv_title.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sf_white));

使用适配器,您可以使用以下代码设置文本颜色:

holder.text_view = (TextView) convertView.findViewById(R.id.text_view);
holder.text_view.setTextColor(Color.parseColor("#FF00FF"));

尝试使用以下代码:

holder.text.setTextColor(Color.parseColor("F00"));
holder.userType.setTextColor(context.getResources().getColor(
                    R.color.green));

试试这个:

TextView textview = (TextView) findViewById(R.id.textview );
textview .setTextColor(Color.parseColor("#85F85F"));