-1

我的代码中有这一行。

String credit= "<font color='#0166b9'>Credit: </font>";

我需要从我的资源中获取“Credit:”字符串,因为它可以根据设备默认语言进行更改。那么我如何在这一行中使用一些变量的值呢?

编辑:

我必须只有一个单词或一些彩色文本。这意味着我必须有不同颜色的 TextView。

有一些方法可以在你的 R.strings 中设置颜色,这里是例子

<string name="clients_credit"> <font fgcolor="#0166b9">Credit: </font> </string>

但它仅适用于最新版本。所以找到一些在string.xml中设置颜色的方法会很棒。

4

5 回答 5

1

使用可以使用 Html.fromHtml();

您可以将 string.xml 中的字符串定义为

<string name="title"><![CDATA[<font color='#0166b9'>Credit: </font>]]></string>

在java文件中你需要使用spannable text

textView.setText(Html.fromHtml(getResources.getString(R.string.title)), TextView.BufferType.SPANNABLE);
于 2013-11-15T08:51:46.997 回答
0

添加 Credit: 到您的strings文件,然后稍后检索它getResources().getString(R.string.credit)并添加颜色 like getResources().getColor(R.color.textColour)。添加

<color name="textColor">#0166b9</color>

到文件夹中的colorXML 。res如果你没有一个创建一个。

于 2013-11-15T08:48:27.773 回答
0

你可以这样做 :

Textview tV;
// Initialize textview 
tV.setText(R.string.credit);
tV.setBackgroundResource(R.color.background_color);
tV.setTextColor(getResources.getColor(R.color.text_color));
于 2013-11-15T08:48:35.707 回答
0

将名为 colors.xml 的文件放在 Android 项目的 res/values 下。

在该文件中声明颜色

<color name="myColor">#ff0166b9<color>

在res/values下的strings.xml中声明字符串

<string name="credit">"Credit: "</string>

在您的 Activity 中,使用

textView = (TextView) findViewById(R.id.idOfTextView);

然后使用以下设置字符串和文本颜色。

textView.setText(R.string.credit);
textView.setTextColor(getResources().getColor(R.color.myColor));

希望这可以帮助。

于 2013-11-15T09:03:22.927 回答
0

我找到了解决方案。就这个。

TextView debitTxtView = (TextView) view.findViewById(R.id.txtViewclientDebit);
final Spannable clientsDebit= new SpannableString(mContext.getString(R.string.clients_debit));
clientsDebit.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.blue)), 0, clientsDebit.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
debitTxtView.setText(clientsDebit);
debitTxtView.append(" " + file.getDebit());
于 2013-11-15T09:48:11.063 回答