Concatenate multiple strings in XML?

前端 未结 10 977
执笔经年
执笔经年 2020-12-01 05:09

I go through this How to concatenate multiple strings in android XML? and in the end there are comments that

For clarity, Its works:

&

相关标签:
10条回答
  • 2020-12-01 05:39

    Yes, you can do so without having to add any Java/Kotlin code, just XML, by using this library: https://github.com/LikeTheSalad/android-string-reference which does that at buildtime.

    For your case, you'd have to set up your strings like this:

    <string name="aaa">Some text</string>
    <string name="template_bbb">bbb ${aaa}</string>
    

    And then the library will create the following at buildtime:

    <string name="bbb">bbb Some text</string>
    

    Disclaimer: I'm the author of this library.

    0 讨论(0)
  • 2020-12-01 05:40

    No, but is posible in xslt file with concat function:

    <html>
          <body>
            <ul>
              <xsl:for-each select="stock">
                <xsl:if test="starts-with(@symbol, 'C')">
                  <li>
                    <xsl:value-of select="concat(@symbol,' - ', name)" />
                  </li>
                </xsl:if>
              </xsl:for-each>
            </ul>
          </body>
        </html>
    

    http://www.java2s.com/Tutorial/XML/0100__XSLT-stylesheet/concatfunction.htm

    0 讨论(0)
  • 2020-12-01 05:44

    Kotlin version:

    strings in the xml:

    <string name="school_girl">Gogo Yubari</string>
    <string name="assistant">Sofie Fatale</string>
    <string name="boss">O-Ren Ishii</string>
    <string name="list">%s, %s, %s</string>
    

    and then in the code:

    val killBillV1 = getString(R.string.list).format(
        Locale.US,
        getString(R.string.school_girl),
        getString(R.string.assistant),
        getString(R.string.boss)
    )
    
    0 讨论(0)
  • 2020-12-01 05:48

    No, you can't concatenate strings in XML but you can define XML resources.

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE resources [
      <!ENTITY appname "MyAppName">
      <!ENTITY author "MrGreen">
    ]>
    
    <resources>
        <string name="app_name">&appname;</string>
        <string name="description">The &appname; app was created by &author;</string>
    </resources>
    

    The original answer was posted here.

    0 讨论(0)
提交回复
热议问题