2017-04-08 44 views
0

我有一个使用文本文件获取文本的应用程序。我可以通过从文件中读取文本并将其附加到文本视图并将其放入Textview中,然后将其放入Textview中,并且工作得很好,但我想向文本中添加格式。像使标题粗体等等。如何将来自文本文件的文本格式设置为Android中的文本视图

private void openFile() 
{ 
    String tema = "Message"; 
    TextView temas = (TextView) findViewById(R.id.tutorial); 
    temas.setText(title); 
    temas.setTextSize(25); 

    temas.setTextColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); 
    try { 
     InputStream is = getAssets().open("tutorials/tutorial1.txt"); 
     int size = is.available(); 

     byte[] buffer = new byte[size]; 
     is.read(buffer); 
     is.close(); 

     String text = new String(buffer); 


     TextView tv = (TextView) findViewById(R.id.text); 
     tv.setText(text); 
     tv.setTextSize(20); 
     tv.setTextColor(Color.parseColor("#000000")); 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 

} 

回答

0

您可以使用HTML标记来显示格式化text.For例如

String x ="Hello <b>World!</b>"; 
rextView.setText(x); 

以获得更多信息。如果你知道你要那么格式化你可以哪个词是指这个答案 here

迭代字符串,直到找到字/字符添加标签并将其添加回字符串。另一种方法是在你想要在文件本身格式化的单词之前添加特殊字符,例如,如果我想让我的标题为粗体,那么我可以在该单词之前添加一个像'#'的字符,所以当我读取我知道的文件时哪些格式我必须适用于哪个单词。

+0

是的,但不适用于文本文件 –

相关问题