【问题标题】:Android TextView not recognising HtmlAndroid TextView 无法识别 Html
【发布时间】:2014-04-07 10:52:05
【问题描述】:

我的应用中有一个 twitter 功能,可以显示特定用户的公共时间线。

我的目标是检测所有主题标签、提及、链接等,并将它们包装在 html 中以更改它们的外观。

到目前为止,这已经奏效,以上所有内容都包含在 Html 中,但它们的外观没有改变。见下文

推文类

public class Tweet {

@SerializedName("created_at")
private String DateCreated;

@SerializedName("text")
private String Text;


public String getDateCreated() {

    SimpleDateFormat format = new SimpleDateFormat("MMM dd,  hh:mm a");

    String date = format.format(Date.parse(DateCreated));

    return date;
}

public StringBuffer getText() {

    Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)");
    Pattern hashtagPattern = Pattern.compile("(#[A-Za-z0-9_-]+)");
    Pattern urlPattern = Patterns.WEB_URL;

    StringBuffer sb = new StringBuffer(Text.length());
    Matcher o = hashtagPattern.matcher(Text);

    while (o.find()) {
        o.appendReplacement(sb, "<font color=\"#437C17\">" + o.group(1) + "</font>");
    }
    o.appendTail(sb);

    Matcher n = mentionPattern.matcher(sb.toString());
    sb = new StringBuffer(sb.length());

    while (n.find()) {
        n.appendReplacement(sb, "<font color=\"#657383\">" + n.group(1) + "</font>");
    }
    n.appendTail(sb);

    Matcher m = urlPattern.matcher(sb.toString());
    sb = new StringBuffer(sb.length());

    while (m.find()) {
        m.appendReplacement(sb, "<font color=\"#EDDA74\">" + m.group(1) + "</font>");
    }
    m.appendTail(sb);

    //textView.setText(Html.fromHtml(sb.toString()));



    return sb;
} 

public void setDateCreated(String dateCreated) {
    DateCreated = dateCreated;
}

public void setText(String text) {
    Text = text;
}


@Override
public String  toString(){


    return  getText()  + "\n"+ getDateCreated() ;

}

    }

我的 Twitter 片段中的 OnPostExecute 方法

        try{
        Twitter twits = jsonToTwitter(result);

        for (Tweet tweet : twits) {


            listview = (ListView) getView().findViewById(R.id.listview);   

            ArrayAdapter<Tweet> adapter = new ArrayAdapter<Tweet>(getActivity(), R.layout.listview_item, twits);


            listview.setAdapter(adapter);

            mProgressDialog.hide();
        }

        }catch(NullPointerException e){

            mProgressDialog.hide();
            System.out.println("No Network Available");

        }
    }

JSON 数据到 twitter 对象方法

        // converts a string of JSON data into a Twitter object
    private Twitter jsonToTwitter(String result) {
        Twitter twits = null;
        if (result != null && result.length() > 0) {
            try {
                Gson gson = new Gson();

                twits = gson.fromJson(result, Twitter.class);

            } catch (IllegalStateException ex) {
                // just eat the exception
            }
        }
        return twits;
    }

感谢您的帮助! :)

【问题讨论】:

    标签: android html string listview twitter


    【解决方案1】:

    Html.fromHtml 不支持font。您可以找到支持的标签列表here

    【讨论】:

    • 感谢您指出这一点。尝试了标签“”,我的问题并不清楚,我尝试实现 Html.fromHtml 但不确定如何或在哪里执行此操作。
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 1970-01-01
    相关资源
    最近更新 更多