Dynamically setting links to text in strings.xml

后端 未结 4 1935
野趣味
野趣味 2020-12-15 11:12

I\'m trying to make an app with localisation built in, but I want a way that I can create a web link within the text, the URL being defined elsewhere (for ease of maintenanc

4条回答
  •  温柔的废话
    2020-12-15 11:18

    The problem is your "a href" link tags are within strings.xml and being parsed as tags when strings.xml is parsed, which you don't want. Meaning you need to have it ignore the tags using XML's CDATA:

    Sample text link1]]>

    And then you can continue with Html.fromHtml() and make it clickable with LinkMovementMethod:

    TextView tv = (TextView) findViewById(R.id.textHolder);
    tv.setText(Html.fromHtml(getString(R.string.sampleText)));
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    

提交回复
热议问题