Android的:如何设置文本的颜色使用字符串 [英] Android: How to Set Color of Text Using a String

查看:673
本文介绍了Android的:如何设置文本的颜色使用字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我带来两个字符串的意图。图1是用于该消息的字符串,而另一个是用于该颜色的串

在code以下显示,2串接收。 消息字符串显示在其正常工作TextView的。然而,我需要字符串messagecolor,设置的TextView的颜色。

我所在的颜色蓝色,绿色和红色中定义一个colors.xml文件

因此​​,如果字符串messagecolor是蓝色的文本将是蓝色。同为红色和绿色。如果字符串messagecolor如果不是蓝色,红色或绿色,则文本将只显示为黑色。

如果有人可以帮我解决这个将是最AP preciated。

感谢您

  //从意图消息
    束束= getIntent()getExtras()。
    字符串messagecolor = bundle.getString(MainActivity.EXTRA_MESSAGE_COLOR);
    字符串消息= bundle.getString(MainActivity.EXTRA_MESSAGE);
    //创建文本视图
    TextView中的TextView =新的TextView(本);
    textView.setTextColor(getResources()的getColor(R.color.blue));
    textView.setTextSize(100);
    textView.setText(消息);


解决方案

而不是试图匹配的颜色有重新$ P $由psented这串,你为什么不通过资源ID本身呢?

I have an intent that brings two strings. 1 is the string for the message and the other is a string for the color.

The code below shows that the 2 strings are received. The "message" string is displayed in the textview which works correctly. However I need the string "messagecolor" to set the color of the textview.

I have a colors.xml file where the colors blue, green and red are defined.

So if the string "messagecolor" is blue to text will be blue. The same for red and green. If the string "messagecolor is not blue,red or green then the text will just appear black.

If someone could help me solve this it would be most appreciated.

Thank you

// Get the message from the intent
    Bundle bundle = getIntent().getExtras();
    String messagecolor = bundle.getString(MainActivity.EXTRA_MESSAGE_COLOR);
    String message = bundle.getString(MainActivity.EXTRA_MESSAGE);


    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextColor(getResources().getColor(R.color.blue));
    textView.setTextSize(100);
    textView.setText(message);

解决方案

Instead of trying to match which colors are represented by which string, why don't you pass the resource ID itself?

这篇关于Android的:如何设置文本的颜色使用字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆