从预生成的html中添加突出显示到span

时间:2012-12-10 16:52:33

标签: java android html

您好我无法弄清楚如何使用styledText字符串中的span,并仅向这些span中的单词添加高亮。 当前的styledText字符串是我从另一个应用程序接收的代码的示例。 跨度的突出显示需要自动发生。 有谁知道如何最好地做到这一点?

    public class ArticleActivity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article);

        TextView textview = new TextView(this);

        String styledText = "Vanwege een verdacht koffertje <span id=\"txt8264:idx8:tkn41:cls1\" class=\"supported dictionary\">is</span> vanochtend een tankstation langs <span id=\"txt8264:idx18:tkn4:cls11\" class=\"supported dictionary\">de</span> A16 bij Hazeldonk korte tijd afgesloten <span id=\"txt8264:idx32:tkn1156:cls1\" class=\"supported dictionary\">geweest</span>.<br/><br/>Volgens <span id=\"txt8264:idx37:tkn4:cls11\" class=\"supported dictionary\">de</span> politie stapte een man uit zijn auto en zette hij <span id=\"txt8264:idx59:tkn98:cls11\" class=\"supported dictionary\">het</span> koffertje naast <span id=\"txt8264:idx65:tkn98:cls11\" class=\"supported dictionary\">het</span> tankstation neer. Daarna reed hij weg.<br/><br/>Een explosievenverkenner van <span id=\"txt8264:idx87:tkn4:cls11\" class=\"supported dictionary\">de</span> politie onderzocht <span id=\"txt8264:idx93:tkn98:cls11\" class=\"supported dictionary\">het</span> koffertje en constateerde dat <span id=\"txt8264:idx103:tkn98:cls11\" class=\"supported dictionary\">het</span> leeg was. <span id=\"txt8264:idx110:tkn66:cls11\" class=\"supported dictionary\">Het</span> tankstation werd korte tijd later weer vrijgegeven. <span id=\"txt8264:idx127:tkn25:cls11\" class=\"supported dictionary\">De</span> afsluiting <span id=\"txt8264:idx131:tkn404:cls1\" class=\"supported dictionary\">had</span> geen gevolgen voor <span id=\"txt8264:idx139:tkn98:cls11\" class=\"supported dictionary\">het</span> verkeer op <span id=\"txt8264:idx145:tkn4:cls11\" class=\"supported dictionary\">de</span> A16 bij <span id=\"txt8264:idx151:tkn4:cls11\" class=\"supported dictionary\">de</span> grensovergang met België.<br/><br/>Ook <span id=\"txt8264:idx162:tkn4:cls11\" class=\"supported dictionary\">de</span> parkeerplaats achter <span id=\"txt8264:idx168:tkn98:cls11\" class=\"supported dictionary\">het</span> tankstation <span id=\"txt8264:idx172:tkn215:cls1\" class=\"supported dictionary\">was</span> bereikbaar.<br/><br/>";

        textview.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);

        setContentView(textview);               
    }  
}

1 个答案:

答案 0 :(得分:0)

我发现最好/唯一的方法是使用带有内联html / css的webview。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView webView = new WebView(this);        
        String styledText = "Vanwege een verdacht koffertje <span id=\"txt8264:idx8:tkn41:cls1\" class=\"supported dictionary\">is</span> vanochtend een tankstation langs <span id=\"txt8264:idx18:tkn4:cls11\" class=\"supported dictionary\">de</span> A16 bij Hazeldonk korte tijd afgesloten <span id=\"txt8264:idx32:tkn1156:cls1\" class=\"supported dictionary\">geweest</span>.<br/><br/>Volgens <span id=\"txt8264:idx37:tkn4:cls11\" class=\"supported dictionary\">de</span> politie stapte een man uit zijn auto en zette hij <span id=\"txt8264:idx59:tkn98:cls11\" class=\"supported dictionary\">het</span> koffertje naast <span id=\"txt8264:idx65:tkn98:cls11\" class=\"supported dictionary\">het</span> tankstation neer. Daarna reed hij weg.<br/><br/>Een explosievenverkenner van <span id=\"txt8264:idx87:tkn4:cls11\" class=\"supported dictionary\">de</span> politie onderzocht <span id=\"txt8264:idx93:tkn98:cls11\" class=\"supported dictionary\">het</span> koffertje en constateerde dat <span id=\"txt8264:idx103:tkn98:cls11\" class=\"supported dictionary\">het</span> leeg was. <span id=\"txt8264:idx110:tkn66:cls11\" class=\"supported dictionary\">Het</span> tankstation werd korte tijd later weer vrijgegeven. <span id=\"txt8264:idx127:tkn25:cls11\" class=\"supported dictionary\">De</span> afsluiting <span id=\"txt8264:idx131:tkn404:cls1\" class=\"supported dictionary\">had</span> geen gevolgen voor <span id=\"txt8264:idx139:tkn98:cls11\" class=\"supported dictionary\">het</span> verkeer op <span id=\"txt8264:idx145:tkn4:cls11\" class=\"supported dictionary\">de</span> A16 bij <span id=\"txt8264:idx151:tkn4:cls11\" class=\"supported dictionary\">de</span> grensovergang met België.<br/><br/>Ook <span id=\"txt8264:idx162:tkn4:cls11\" class=\"supported dictionary\">de</span> parkeerplaats achter <span id=\"txt8264:idx168:tkn98:cls11\" class=\"supported dictionary\">het</span> tankstation <span id=\"txt8264:idx172:tkn215:cls1\" class=\"supported dictionary\">was</span> bereikbaar.<br/><br/>";
        final WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        StringBuilder html = new StringBuilder();
        html.append("<html>");
        html.append("<head>");
        html.append("<style type='text/css'>  body .supported{background-color:#00A4E4;}  </style>");
        html.append("</head>");
        html.append("<body>");
        html.append("<h1>Some Heading</h1>");
        html.append(styledText.toString());
        html.append("</body></html>");
        webView.loadDataWithBaseURL("file:///android_asset/", html.toString(), "text/html", "UTF-8", "");           
        setContentView(webView);
        }