更改android中每行的颜色

时间:2013-04-22 12:23:04

标签: android colors textview

您好, 我用了这段代码:

String displayText = "";
    try {
        InputStream fileStream = getResources().openRawResource(
                            R.raw.t);
        int fileLen = fileStream.available();
        // Read the entire resource into a local byte buffer.
        byte[] fileBuffer = new byte[fileLen];
        fileStream.read(fileBuffer);
        fileStream.close();
        displayText = new String(fileBuffer);
        } catch (IOException e) {
          // exception handling
        }

    TextView tv=(TextView) findViewById(R.id.textView1);
    tv.setText(displayText);


}

用于显示文本文件。 我想改变每一行的颜色(并改变行间颜色) 我该怎么办?

我很抱歉说英语不好

1 个答案:

答案 0 :(得分:2)

我建议您使用html设置textview中的颜色。一个在两种颜色之间交换的小例子:

String textcontent = ""; 
String[] lines;
//something like "lines = displayText.split("\n")" or however you stored line returns
for (int i = 0; i < lines.length; i++) {
    //odd line numbers are red, the even ones green
    textcontent += "<font color=\"" + (i%2==0?"red":"green") + "\">" + lines[i] + "</font><br/>";
}
TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText(Html.fromHtml(textcontent));

我希望这有所帮助,否则可以随意提出更多......