Android: html in strings.xml

后端 未结 5 1968
谎友^
谎友^ 2020-11-29 21:10

I would like display for example this html code:


    

Hello World

This is a test of the URL &

5条回答
  •  一个人的身影
    2020-11-29 21:56

    Here's most of the examples. I don't think the pre tag is supported.

    enter image description here

    This is the strings.xml file:

    
    
        Formatting
        <b>Hello World</b> This is a test of the URL <a href="http://www.example.com/">Example</a>
        <b>This text is bold</b>
        <em>This text is emphasized</em>
        This is <sub>subscript</sub> and <sup>superscript</sup>
    
    

    Here's the layout. Note for the link to actually be clickable, there's a bit of extra work needed:

    
    
    
    
        
            
            
            
            
        
    
    

    Finally, the code:

    TextView test1 = (TextView)findViewById(R.id.test1);
    Spanned spanned = Html.fromHtml(getString(R.string.link));
    test1.setMovementMethod(LinkMovementMethod.getInstance());
    test1.setText(spanned);
    
    TextView test2 = (TextView)findViewById(R.id.test2);
    test2.setText(Html.fromHtml(getString(R.string.bold)));
    
    TextView test3 = (TextView)findViewById(R.id.test3);
    test3.setText(Html.fromHtml(getString(R.string.emphasis)));
    
    TextView test4 = (TextView)findViewById(R.id.test4);
    test4.setText(Html.fromHtml(getString(R.string.sup)));
    

提交回复
热议问题