【问题标题】:To change the color for each line in android在android中更改每一行的颜色
【发布时间】:2013-04-22 12:23:04
【问题描述】:

嗨, 我使用了这段代码:

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


}

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

对不起,英语不好

【问题讨论】:

    标签: android colors textview


    【解决方案1】:

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

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

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

    【讨论】:

    • &我可以这样使用自定义字体吗?
    • 当然,像 face=\"Georgia\" 这样的东西应该可以解决问题。查看this info 了解有关字体标签的更多信息,但其他一些 html 标签也可以使用。快乐编码 =)
    猜你喜欢
    • 1970-01-01
    • 2011-08-06
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2021-06-26
    • 2014-10-07
    • 1970-01-01
    相关资源
    最近更新 更多