首页 > 解决方案 > 如何在strings.xml中使用href

问题描述

strings.xml 中的字符串

<string name="lblAuthor1">Icon made by  <a href="https://www.flaticon.com/authors/smashicons">Smashicons</a> from
         <a href="https://www.flaticon.com">www.flaticon.com</a></string>

活动中

TextView lblAuthor1 = findViewById(R.id.lblAuthor1);

lblAuthor1.setText(getText(R.string.lblAuthor1));
lblAuthor1.setMovementMethod(LinkMovementMethod.getInstance());

这个答案中,我读到

我应该只使用<b>, <i> and <u>文档中列出的它们。

但由于某种原因,a href tag我的应用程序中的作品,所以我想知道这是否会导致任何问题,因为文档不包括a href在 strings.xml 中使用它时支持的内容

标签: javaandroid

解决方案


方法一

像这样更改您的代码

   if (Build.VERSION.SDK_INT >= 24) {
        lblAuthor1.setText(Html.fromHtml(getText(R.string.lblAuthor1), Html.FROM_HTML_MODE_LEGACY));
    } else {
        lblAuthor1.setText(Html.fromHtml(getText(R.string.lblAuthor1)));
    }

代替

lblAuthor1.setText(getText(R.string.lblAuthor1));

方法二

关注这个答案


推荐阅读