How to have “&” i.e and symbol in Android TextView

后端 未结 8 468
半阙折子戏
半阙折子戏 2020-12-24 11:51

I want to have some simple text in textView..

Like...(android & java)

means I exactly require \"and\" symbol in textView

If I\'m pro

相关标签:
8条回答
  • 2020-12-24 12:15

    Define a new string in strings.xml with the value of "Bakeries & Dessert".

    Then, in the layout declare

    <TextView android:text="@string/your_string_value"></TextView>

    Later edit:

    Adding an invalid character for XML in another XML file to circumvent the limitation will obviously not work. However, if you use Eclipse, when you add a new value in strings.xml, the characters like &, < and > will be automatically replaced by the HTML equivalents (&amp; &lt; &gt;).

    0 讨论(0)
  • 2020-12-24 12:16

    Another way to solve this is using Unicode:

    1. set your text in your xml to

      android:text="@string/myNameHere"

    2. In strings xml add the following line:

      <string name="myNameHere">bakeries \u0026 desserts</string>

    voilá!

    0 讨论(0)
  • 2020-12-24 12:17

    Try to set the text by code

    TextView tv = (TextView)findViewById(R.id.textview);
    tv.setText("input");
    

    using Html to encode/decode the text

    Html.fromHtml(string) 
    
    0 讨论(0)
  • 2020-12-24 12:23

    The layout file is an XML file and has certain restrictions on which characters may be used. However, you can use XML escape sequences to display characters which have special meaning to XML. In this case, replace the "Bakeries & Dessert" with the string "Bakeries &amp; Dessert" and you should get the behaviour that you require.

    0 讨论(0)
  • 2020-12-24 12:27

    The layout files are just xml and the & symbol is not allowed by itself.

    You will need to use &amp; instead

    http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML

    0 讨论(0)
  • 2020-12-24 12:33

    Simply write it with amp followed by semi-colon as below:

    android:text="Bakeries &amp; Dessert"
    
    0 讨论(0)
提交回复
热议问题