1

嗨,我使用了以下代码:

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);


}

它用于显示文本文件。我想改变每条线的颜色(并改变线之间的颜色)我应该怎么做?

我很抱歉说英语不好

4

1 回答 1

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));

我希望这会有所帮助,否则请随时要求更多...

于 2013-04-22T12:57:35.407 回答