如何读取文本文件,并改变颜色和奇数行的字体大小,甚至线
问题描述:
你好 我有一个文本文件,并想读取并显示成不同的字体和颜色的TextView神的名字奇数行和偶数行,如何改变这种阅读代码:
代码:
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"komeil.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
text.append('\n');
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
这样的文本文件:
文件:
hello people i am student //black
i want to rad this file //green
different color //black
different font //green
how to do this //black
... //green
... //black
. //green
. //black
. //green
答
它在textview中是不可能的,因为textview只接受一种颜色。为此,您必须使用webview,使用不同颜色的线创建html页面
请参阅http://stackoverflow.com/questions/1529068/is-it-possible-to-have-multiple-styles-inside-a -textview – comodoro 2012-07-27 17:06:01
当将文本添加到文本视图中时,您可以使用TextAppearanceSpans ...,而不是插入字符串,而是使用可显示样式字符串的Spans。 http://developer.android.com/reference/android/text/style/TextAppearanceSpan.html – 2012-07-27 17:07:35
我是业余程序员,如果你纠正这个代码,我很感谢! – hossein 2012-07-27 18:46:20