如何将HTML添加到TextView?

时间:2015-10-18 16:07:25

标签: android textview

如何在TextView上添加html页面最终TextView titleTextView =(TextView)?

private void renderViewIngredients()
{
    // reference
    final TextView titleTextView = (TextView) mRootView.findViewById(R.id.fragment_ebook_detail_ingredients_title);
    final ViewGroup containerViewGroup = (ViewGroup) mRootView.findViewById(R.id.fragment_ebook_detail_ingredients_container);
    final Button clearButton = (Button) mRootView.findViewById(R.id.fragment_ebook_detail_ingredients_clear);
    final Button recalculateButton = (Button) mRootView.findViewById(R.id.fragment_ebook_detail_ingredients_recalculate);

    // title
    if(mRecalculatedServings==mRecipe.getServings())
    {
        titleTextView.setText(R.string.fragment_ebook_detail_ingredients_title);
    }
    else
    {
        titleTextView.setText(getString(R.string.fragment_ebook_detail_ingredients_title_recalculated, mRecalculatedServings));
    }

2 个答案:

答案 0 :(得分:1)

  

如何在TextView上添加html页面

由于您从String获得strings.xml,我建议您使用CDATAsection,如下所示:

<string name="fragment_ebook_detail_ingredients_title"><![CDATA[<p><b>YourEbookDetailWithBold</b><p>]]></string>

然后,当您想要使用它时,您必须使用Html.fromHtml()

titleTextView.setText(Html.fromHtml(getString(R.string.fragment_ebook_detail_ingredients_title)));

修改

当我看到:

  

如何在TextView上添加html页面

我想到了一个链接html,所以如果你有一个链接,请按照这些步骤进行操作,如果不能阅读更多内容。

确保TextView中的xml有此

android:linksClickable="true"

然后您必须创建Spanned

Spanned spannedlink = Html.fromHtml(getString(R.string.fragment_ebook_detail_ingredients_title));

然后添加以下行:

titleTextView.setMovementMethod(LinkMovementMethod.getInstance());

并在titleTextView

上设置文字
titleTextView.setText(spannedlink );

答案 1 :(得分:0)

如果你想使用带有TextView的html标签,你应该使用:

titleTextView.setText(Html.fromHtml("your html string"));