开发者

TextView setTextColor() not working

I programmatically create a list (no a ListView, just adding them to the parent) of such elements:

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:orientation="vertical" android:layout_weight="1">
    <TextView android:id="@+id/filiale_name"
    android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/lagerstand_text"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:textSize="10sp" android:textColor="@col开发者_开发百科or/red"/>
</LinearLayout>

Also, I have defined some colors in values/colors.xml. As you see, the TextView with id "lagerstand_text" has set it's color to red by default. That works.

When creating the elements in Java, I do

lagerstandText.setText("bla");

and for some elements also I do

lagerstandText.setTextColor(R.color.red);

and other colors. While the elements on which I don't call setTextColor() are red, all others are grey, no matter which color I chose (even if it's the same red again).

Why is that?


The documentation is not very verbose about this, but you cannot use just the R.color integer when calling setTextColor. You need to call getResources().getColor(R.color.YOURCOLOR) to set a color properly.

Use the following to set color of your text programmatically:

textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));

Starting with the support library 23 you have to use the following code, because getColor is deprecated:

textView.setTextColor(ContextCompat.getColor(context, R.color.YOURCOLOR));


So, there are many ways to achieve this task.

1.

int color = Integer.parseInt("bdbdbd", 16)+0xFF000000;
textview.setTextColor(color);

2.

textView.setTextColor(getResources().getColor(R.color.some_color));

3.

textView.setTextColor(0xffbdbdbd);

4.

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

5.

textView.setTextColor(Color.argb(a_int, r_int, g_int, b_int));


1.standard color u prefer please go with below .

textview.setTextColor(Color.select_color)

2.here want to use custwom color add it in color.xml file

textview.setTextColor(getResources().getColor(R.color.textbody));

or

textView.setTextColor(Color.parseColor("#000000"));

or

subText.setTextColor(Color.rgb(255,192,0));


For future reference, you can use the follow:

String color = getString(Integer.parseInt(String.valueOf(R.color.my_color)));
my_textView.setTextColor(Color.parseColor(color));

This way you can make use of your Color Resources.


textView.setTextColor(Color.RED);


The integer id for a particular color(defined in xml layout) defined in R class cannot be passed as a parameter to setTextColor() method of View class. You must obtain the parameter of the setTextColor() by the following line of code :

int para=getResources().getColor(R.color.your_color,null);
view.setTextColor(para,null);

The method getColor(int id) has been depreciated...instead use getColor(int id,Resources.Theme theme) as in the line of code above.

The `second parameter( theme )` can be null
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜