Android 在 XML 中连接多个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10411699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:37:30  来源:igfitidea点击:

Concatenate multiple strings in XML?

androidxmlstring

提问by Lukap

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

我经历了这个如何在 android XML 中连接多个字符串?最后有评论说

For clarity, Its works:

For clarity, Its works:

<string name="title">@string/app_name</string>.Andrzej Du?

<string name="title">@string/app_name</string>.安杰伊杜?

I made my own example but it doesn't works. So does Andrzejwrong or I am doing something wrong in my code.

我做了我自己的例子,但它不起作用。所以确实安杰错误或我在我的代码做错了什么。

R.strings.bbbshould contains "bbb aaa"but instead of "bbb aaa"it contains "bbb @strings/aaa"

R.strings.bbb应该包含"bbb aaa"但不是"bbb aaa"它包含"bbb @strings/aaa"

<string name="aaa">aaa</string>
<string name="bbb">bbb @strings/aaa</string>

Query:

询问:

Is it possible to do some concatenation only in xml, without source code changes?

是否可以只在 xml 中进行一些连接,而无需更改源代码?

Reason why I don't want to edit in code because I use this strings in xml/preferences.xml

我不想在代码中编辑的原因,因为我在 xml/preferences.xml

For Example:

例如:

<ListPreference android:key="key_bbb" android:title="@string/bbb"....

<ListPreference android:key="key_bbb" android:title="@string/bbb"....

If you know what I mean, here there is no possibility to use something like this

如果你知道我的意思,这里不可能使用这样的东西

String title = res.getString(R.string.title, appName);

采纳答案by Lalit Poptani

No I don't think you can concatenate.

不,我认为你不能连接。

<string name="aaa">aaa</string>
<string name="bbb">bbb @string/aaa</string>

Output - bbb @string/aaa

输出 - bbb @string/aaa

If you do,

如果你这样做,

<string name="aaa">aaa</string>
<string name="bbb">@string/aaa bbb</string>  -> This won't work it
                                                      will give compilation error

Because here it will search for a String with reference @string/aaa bbbwhich does not exists.

因为在这里它将搜索一个@string/aaa bbb不存在的带有引用的字符串。

Problem in your case was, you where using @strings/aaawhich should be @string/aaa

在您的情况下,问题是,您@strings/aaa应该在哪里使用@string/aaa

回答by Savelii Zagurskii

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

不,您不能连接 XML 中的字符串,但您可以定义 XML 资源。

<?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.

原来的答案张贴在这里

回答by Dharmendra

In XML only this is not possible but using the java code you can use the String.format()method.

仅在 XML 中这是不可能的,但使用 java 代码可以使用该String.format()方法。

<string name="aaa">aaa</string>
<string name="bbb">bbb %1$s</string>

In java code

在java代码中

String format = res.getString(R.string.bbb);
String title = String.format(format, res.getString(R.string.aaa));

So title will be a full string after concatenation of two strings.

所以标题将是两个字符串连接后的完整字符串。

回答by Boy

You can concatenate the resources in gradle.build:

您可以连接 gradle.build 中的资源:

    resValue "string", "TWITTER_CALLBACK", "twitter_callback_" + applicationId

回答by batsheva

Yes, you can do it if your XML files are using DataBinding as you can see in this link

是的,如果您的 XML 文件使用数据绑定,您可以这样做,如您在此链接中所见

Use it like this:

像这样使用它:

"@{@string/first_string+ @string/second_string}"

回答by César Mu?oz

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-referencewhich does that at buildtime.

是的,您可以这样做而无需添加任何 Java/Kotlin 代码,只需使用此库:https: //github.com/LikeTheSalad/android-string-reference,它在构建时执行此操作。

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.

免责声明:我是这个库的作者。

回答by netpork

Kotlin version:

科特林版本:

strings in the xml:

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)
)

回答by Waqar Aslam

How about this technique

这个技术怎么样

Use an array

使用数组

<string name="app_link">https://myapp.com/</string>

<array name="update_dialog_msg">
    <item>Update can be downloaded manually by visiting this link:\n</item>
    <item>@string/app_link</item>
</array>

And then make a method in java class as below

然后在java类中创建一个方法如下

public String getStringArray(int resStringArray)
{
    String str = "";

    String[] strArray = mContext.getResources().getStringArray(resStringArray);
    for (String s: strArray)
    {
        str += s;
    }

    return str;
}

Now call it from any java class as

现在从任何 java 类调用它作为

mDialog.setMessage(getStringArray(R.array.update_dialog_msg);
// OR
textView.setText(getStringArray(R.array.update_dialog_msg);

回答by Ejrr1085

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

不,但可以在带有 concat 函数的 xslt 文件中使用:

<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

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