如何设置HTML从资源到TextView? [英] java android - how to set html from resources to TextView?

查看:128
本文介绍了如何设置HTML从资源到TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可能将res / raw加载到TextView中?
我知道我可以使用WebView,但该死的透明并不总是正常工作(而不是每个设备上)。 p> myTextView.setText(Html.fromHtml(readTxt()));

//此函数将返回您可以在textview中设置的字符串。并且该String具有HTML代码,所以使用 Html.fromHtml

  private String readTxt(){
InputStream inputStream = getResources()。openRawResource(R.raw.My_html_file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
尝试{
i = inputStream.read();
while(i!= -1){
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
返回byteArrayOutputStream.toString();
}


Is there any possibility to load html from res/raw into the TextView? I know I can use WebView, but damn transparency is not always working (not on every device)

解决方案

myTextView.setText(Html.fromHtml(readTxt()));     

//This function will return string which you can set in your textview. And that String have html codes so use Html.fromHtml

 private String readTxt() {
    InputStream inputStream = getResources().openRawResource(R.raw.My_html_file);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i;
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return byteArrayOutputStream.toString();
  }

这篇关于如何设置HTML从资源到TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆