如何在strings.xml中引用其他字符串?

时间:2019-02-07 19:57:54

标签: android xml string

我想在另一个字符串中引用一个字符串,所以我只需要在一个地方进行更改。

我不想从Java处理此问题。也就是说,我不想使用%s和(getString(R.string.log_in_to_my_app),appName)

例如,我只想在第一个字符串中看到“ MyApp”,而在其他字符串中看到它的引用:

<string name="app_name">MyApp</string>
<string name="log_in_to_my_app">Log In To MyApp</string>
<string name="login_with_my_app">Log in with MyApp</string>

所以也许像这样:

<string name="app_name">MyApp</string>
<string name="log_in_to_my_app">Log In To @app_name</string>
<string name="login_with_my_app">Log in with @app_name</string>

2 个答案:

答案 0 :(得分:0)

您可以这样称呼字符串:

android:text =“ @ string / app_name”

android:text =“ @ string / log_in_to_my_app”

android:text =“ @ string / login_with_my_app”

答案 1 :(得分:0)

答案在https://stackoverflow.com/a/39870268/6558369

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