Java 如何在string.xml中使用两个同名的不同字符串值?

Java 如何在string.xml中使用两个同名的不同字符串值?,java,android,Java,Android,我想有两种消息类型,例如讽刺和官方消息。当用户选择讽刺时,所有消息以讽刺方式显示;当用户选择正式时,所有消息以正式方式显示。我想在string.xml中使用两个名称相同但值不同的字符串值类型。我想选择应用程序运行时将在程序中使用的类型。还有别的办法吗。 谢谢您的帮助。string.xml中不能有两个具有相同键或名称的不同字符串 您可能可以将app_name和app_name_1设置为讽刺和正式,只要正式将app_1添加到string中,string.xml中就不能有两个具有相同键或名称的不同字符

我想有两种消息类型,例如讽刺和官方消息。当用户选择讽刺时,所有消息以讽刺方式显示;当用户选择正式时,所有消息以正式方式显示。我想在string.xml中使用两个名称相同但值不同的字符串值类型。我想选择应用程序运行时将在程序中使用的类型。还有别的办法吗。
谢谢您的帮助。

string.xml中不能有两个具有相同键或名称的不同字符串


您可能可以将app_name和app_name_1设置为讽刺和正式,只要正式将app_1添加到string

中,string.xml中就不能有两个具有相同键或名称的不同字符串


您可能可以将app_name和app_name_1设置为讽刺和官方,只要官方将app_1添加到字符串

中,如果您有一条消息,请以字符串xml创建两条消息,并根据需要进行设置;如果有消息数组,则可以以字符串xml创建字符串数组,并根据用户选择在小部件中进行设置


例如,如果用户选择“官方消息”,则可以使用“官方消息”的“消息数组”填充微调器,或者使用“讽刺消息”填充微调器。如果您有一条消息,请创建两条字符串xml消息,并根据需要进行设置;如果有消息数组,则可以使用字符串xml创建字符串数组,并根据用户在小部件中进行设置选择


例如,如果用户选择官方消息,您可以用消息\u-array\u of\u-official填充微调器,或者用反讽消息填充微调器。我从您的问题中得出的结论是,您希望相同名称的多个字符串值-字符串数组

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="message">
      <item>Ironic</item>
      <item>Official</item>
  </string-array>
</resources>

我从您的问题中得出的结论是,您希望为同一名称字符串数组使用多个字符串值

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="message">
      <item>Ironic</item>
      <item>Official</item>
  </string-array>
</resources>

您不能切换字符串资源名称的含义,但可以在字符串的预构建映射集之间切换:

Resources res = getResources();

HashMap<Integer, String> ironic = new HashMap<Integer, String>()
ironic.put(R.string.official_hello, res.getString(R.string.ironic_hello);
ironic.put(R.string.official_goodbye, res.getString(R.string.ironic_goodbye);

HashMap<Integer, String> official = new HashMap<Integer, String>()
ironic.put(R.string.official_hello, res.getString(R.string.official_hello);
ironic.put(R.string.official_goodbye, res.getString(R.string.official_goodbye);
Resources res=getResources();
HashMap ironic=新的HashMap()
put(R.string.official\u hello,res.getString(R.string.ironic\u hello);
反讽。put(R.string.official_再见,res.getString(R.string.反讽_再见);
HashMap official=newhashmap()
讽刺的。put(R.string.official\u hello,res.getString(R.string.official\u hello);
讽刺的。put(R.string.official_再见,res.getString(R.string.official_再见);
现在,您可以使用ironic版本资源整数查找ironic或official。您只需做出一个选择(可能在活动中)即可使用正确的映射,并将其与官方字符串资源ID一起使用,以编程方式填充TextView对象:

HashMap<Integer, String> strings;
if (is_ironic) {
    strings = ironic;
} else {
    strings = official;
}

TextView tv_hello = ...
tv_hello.setText(strings.get(R.string.official_hello));
HashMap字符串;
如果(是讽刺){
弦乐=讽刺;
}否则{
字符串=官方;
}
TextView tv_hello=。。。
tv_hello.setText(strings.get(R.string.official_hello));

您不能切换字符串资源名称的含义,但可以在字符串的预构建映射集之间切换:

Resources res = getResources();

HashMap<Integer, String> ironic = new HashMap<Integer, String>()
ironic.put(R.string.official_hello, res.getString(R.string.ironic_hello);
ironic.put(R.string.official_goodbye, res.getString(R.string.ironic_goodbye);

HashMap<Integer, String> official = new HashMap<Integer, String>()
ironic.put(R.string.official_hello, res.getString(R.string.official_hello);
ironic.put(R.string.official_goodbye, res.getString(R.string.official_goodbye);
Resources res=getResources();
HashMap ironic=新的HashMap()
put(R.string.official\u hello,res.getString(R.string.ironic\u hello);
反讽。put(R.string.official_再见,res.getString(R.string.反讽_再见);
HashMap official=newhashmap()
讽刺的。put(R.string.official\u hello,res.getString(R.string.official\u hello);
讽刺的。put(R.string.official_再见,res.getString(R.string.official_再见);
现在,您可以使用ironic版本资源整数查找ironic或official。您只需做出一个选择(可能在活动中)即可使用正确的映射,并将其与官方字符串资源ID一起使用,以编程方式填充TextView对象:

HashMap<Integer, String> strings;
if (is_ironic) {
    strings = ironic;
} else {
    strings = official;
}

TextView tv_hello = ...
tv_hello.setText(strings.get(R.string.official_hello));
HashMap字符串;
如果(是讽刺){
弦乐=讽刺;
}否则{
字符串=官方;
}
TextView tv_hello=。。。
tv_hello.setText(strings.get(R.string.official_hello));

从技术上讲,你说的是两种语言“讽刺”和“官方”。 Android允许自定义语言,所以只需在两个不同的资源目录中创建两个不同的字符串文件,比如values-irn/strings.xml和values-off/strings.xml

然后在或应用程序的活动的onCreate()中,执行以下操作:

Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = new Locale("irn"); // or "off"
resources.updateConfiguration(configuration, resources.getDisplayMetrics());

这应该可以做到。

从技术上讲,你说的是两种语言“讽刺”和“官方”。 Android允许自定义语言,所以只需在两个不同的资源目录中创建两个不同的字符串文件,比如values-irn/strings.xml和values-off/strings.xml

然后在或应用程序的活动的onCreate()中,执行以下操作:

Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = new Locale("irn"); // or "off"
resources.updateConfiguration(configuration, resources.getDisplayMetrics());

这应该可以做到。

你可以重新定位。有很多本地化教程,所以我只会在理论上讲。这是和

优点:代码非常清晰

缺点:您可能必须以编程方式重新启动应用程序

这个想法是通过使用非ISO标准国家代码作为讽刺语的语言,并使用标准国家代码作为官方语言

您应该在res中创建两个文件夹,如下所示:

价值观(这是官方的)

值rc(这将是一个讽刺)

并将string.xml放入两个文件夹中

官方(价值观)

这样,当用户选择Official时,它将在values下显示get string from strings.xml,当用户选择Ironic时,它将在values rc下显示get string

这样你就有了一个非常清晰的结构。唯一的缺点是你可能需要重新启动应用程序才能生效

您可以使用此代码重新启动活动

Intent intent = getActivity().getIntent();
intent.putExtra("from_setting", true);
getActivity().finish();
startActivity(intent);

你可以重新定位。有很多本地化教程,所以我只在理论上讲。这里是和

优点:代码非常清晰

缺点:您可能必须以编程方式重新启动应用程序

这个想法是通过使用非ISO标准国家代码作为讽刺语的语言,并使用标准国家代码作为官方语言

您应该在res中创建两个文件夹,如下所示:

价值观(这是官方的)

值rc(该值为o