我想制作一个标记信息窗口,它有3行,每条单行包含2种颜色。 我在
的帮助下制作了3行 class MyTask extends AsyncTask<Void,Void,String>
{
EditText et1 = (EditText)findViewById(R.id.et1);
public String doInBackground(Void... unused)
{
HttpURLConnection conn = null;
try
{
URL url = new URL("http://www.free-map.org.uk/course/mad/ws/hits.php?artist=",et1);
conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
if(conn.getResponseCode() == 200)
{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String result = "", line;
while((line = br.readLine()) !=null)
result += line;
return result;
}
else
return "HTTP ERROR: " + conn.getResponseCode();
}
catch(IOException e)
{
return e.toString();
}
finally
{
if(conn!=null)
conn.disconnect();
}
}
但我对颜色很感兴趣。
请帮助:)
答案 0 :(得分:0)
您可以使用 SpannableString 在TextView
或其他启用文字的组件中的单个字符串句子中设置多种颜色
String strMessage="I am android developer";
Spannable spannable = new SpannableString(strMessage);
//"I am android" red colored
spannable.setSpan(getTextAppearanceSpan(ContextCompat.getColor(mContext, android.R.color.RED)), 0, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
//"developer" blue colored
spannable.setSpan(getTextAppearanceSpan(ContextCompat.getColor(mContext, android.R.color.BLUE)), 14, strMessage.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
TextAppearanceSpan 方法,以自定义文字颜色
/*
*get customized text with color, style
*/
private TextAppearanceSpan getTextAppearanceSpan(int color) {
ColorStateList blueColor1 = new ColorStateList(new int[][]{new int[]{}}, new int[]{color});
return new TextAppearanceSpan(null, Typeface.NORMAL, -1, blueColor1, null);
}