Java 以编程方式在文本视图中使用的字符串颜色
是否可以通过编程方式设置文本视图中使用的字符串的颜色?我试过用这个,但没用Java 以编程方式在文本视图中使用的字符串颜色,java,android,textview,Java,Android,Textview,是否可以通过编程方式设置文本视图中使用的字符串的颜色?我试过用这个,但没用 TextView txt = (TextView)v.findViewById(R.id.textView1); txt.setText(Html.fromHtml("<font color='#0099CC'>@string/app_name</font>" + "<font color='#995676'> @stri
TextView txt = (TextView)v.findViewById(R.id.textView1);
txt.setText(Html.fromHtml("<font color='#0099CC'>@string/app_name</font>" +
"<font color='#995676'> @string/app_description</font>" +
"<font color='#55GGFES'> @string/app_version</font>"
));
你可以,但这并不容易。您不能以这种方式将@string/插入Java中的字符串中。您可以使用的是context.getResources.getStringR.id.app\u name;要知道名字。所以像这样:
txt.setText(Html.fromHtml("<font color='#0099CC'>"+
context.getResources().getString(R.id.app_name)+"</font>"+
"<font color='#995676'>"+
context.getResources().getString(R.id.app_description)+"</font>"));
您也可以使用Spanable
String mString = context.getResources().getString(R.id.app_name);
Spannable coloredSpan = new SpannableString(mString);
//Spannable.setSpan(Object what, int start, int end, int flags)
coloredSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, mString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txt.setText(coloredSpan);
不,因为我需要在一个文本视图中使用多个字符串。我们还有一个问题,实际上是另一个问题的副本:对不起,没有完全仔细阅读。如果它是副本,请将其删除。你能展示两个字符串的用法吗?太棒了!谢谢:-