1

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

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

1 回答 1

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);
        }
于 2013-01-03T13:03:47.573 回答