Android:为以编程方式创建的 TextView 设置文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22983772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:38:56  来源:igfitidea点击:

Android: Set textcolor for programmatically created TextView

androidtextview

提问by W I Z A R D

I have created TextViewprogrammatically, Now i want to set text color to the TextViewbelow is my code

我已经以TextView编程方式创建,现在我想将文本颜色设置为TextView下面是我的代码

TableLayout ll = (TableLayout) findViewById(R.id.auditContent);
public TableRow row;
TextView txtNumber;

for (int i = 0; i < ItemCount; i++) {
row = new TableRow(MainActivity.this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
                    row.setLayoutParams(lp);
                    row.setWeightSum(1f);
      txtNumber = new TextView(MainActivity.this);
      txtNumber.setGravity(Gravity.CENTER);
      txtNumber.setText("No." + count);
      txtNumber.setTextColor(getResources().getColor(R.color.blue)); //setting text color

 row.addView(txtNumber);

ll.addView(row, i);
    }

The textcoloris not setting the color to TextViewtext, m doing anything wrong, And i debug the code there is no error. Please help thanks

textcolor没有颜色设置为TextView文本,男做错什么,我调试没有错误的代码。请帮忙谢谢

In string.xml <color name="blue">#33CCCC</color>m not using color.xml The above color will work fine for xml TextView

在 string.xml <color name="blue">#33CCCC</color>m 不使用 color.xml 上面的颜色对 xml 工作正常TextView

回答by Piyush

According to your xml file you need to change

根据您的 xml 文件,您需要更改

txtNumber.setTextColor(getResources().getColor(R.color.blue));

to

txtNumber.setTextColor(getResources().getString(R.color.blue));

Further more you can make color.xmlfile in your valuesfolder and in that use

此外,您可以在color.xml文件values夹中创建文件并使用

<color name="mycolor">#33CCCC</color>

now just use this way

现在就用这种方式

txtNumber.setTextColor(getResources().getColor(R.color.mycolor));

回答by Parth Vora

Starting from Android Support Library 23

从 Android 支持库 23 开始

txtNumber.setTextColor(ContextCompat.getColor(context, R.color.your_color));

回答by Sharanjeet Kaur

tv_name.setTextColor(Color.parseColor("#bdbdbd"));

tv_name.setTextColor(Color.parseColor("#bdbdbd"));

回答by Vikram Kodag

Use this function to set TextView color programmatically

使用此函数以编程方式设置 TextView 颜色

private void setViewColor(TextView inputTextView, int colorId) {
    //From API 23, getResources().getColor() is deprecated
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        inputTextView.setTextColor(ContextCompat.getColor(context, colorId));
    } else {
        inputTextView.setTextColor(context.getResources().getColor(colorId));
    }
}

回答by Shriram

USe

text.setTextColor(Color.rgb(200,0,0));
setTextColor(Color.parseColor("#FFFFFF"));
text.setTexColor(getResources().getColor()(R.color.colorname)

make sure your resources will be

确保您的资源

#eaeaea

#eaeaea

回答by Pankaj Arora

//define global

//定义全局

int color;

// in on create

// 在创建时

color = Integer.parseInt("YOUR COLOR CODE", 16)+0xFF000000;
{
 txtNumber = new TextView(MainActivity.this);
 txtNumber.setGravity(Gravity.CENTER);
 txtNumber.setTextColor(color); //setting text color
}

回答by Naval Sharma

Use this to change text color :

使用它来更改文本颜色:

textview.setTextColor(new Color().parseColor("#ffffff"));