How to add < and> in android strings.xml

前端 未结 3 1024
陌清茗
陌清茗 2021-01-11 11:38

I need to add in strings.xml. What is the escape sequence in order to do that? The string to

相关标签:
3条回答
  • 2021-01-11 11:45

    Use &lt; for less than symbol and &gt; for greater than symbol. so here you need to replace like:

    <string name="name_hint">&lt;Given Name&gt;</string>
    
    0 讨论(0)
  • 2021-01-11 11:48

    The Android string.xml file is just a regular XML file, so you can use the XML escape characters:

    "   &quot;
    '   &apos;
    <   &lt;
    >   &gt;
    &   &amp;
    

    Your example would become <string name="name_hint">&lt;Given Name&gt;</string>

    UPDATE: Furthermore you can use the HTML entities of each character, a blank space is for example &#032;, and a non-breaking space is &#160;.

    Note that I am not entirely sure whether this is officially supported by the XML standards, or that it 'just works'...

    0 讨论(0)
  • 2021-01-11 11:59

    You can use "<" for "<" and ">" for ">"
    So you should add the following string in your app's strings.xml like:

    <string name="name_hint">&lt;Given Name&gt;</string>
    

    For your another requirement you can define it like

    <Given name> <Middlename> <Sirname>

    <string name="name_hint">&lt;Given Name&gt; &lt;Middle Name&gt; &lt;Last Name&gt; </string>
    

    or define three different strings.

    <string name="name_hint1">&lt;Given Name&gt;</string>
    <string name="name_hint2">&lt;Middle Name&gt;</string>
    <string name="name_hint3">&lt;Last Name&gt;</string>
    

    in your java file add it dynamically

    tv.setText(getResources().getString(R.string.name_hint1) + "  " +
      getResources().getString(R.string.name_hint2)+"  " + 
      getResources().getString(R.string.name_hint3));
    
    0 讨论(0)
提交回复
热议问题