如何为 ListView 中的 ListContents 设置不同的语言

发布于 2024-12-07 16:17:32 字数 990 浏览 0 评论 0 原文

我有两个简单的 ListViews:

public class SimpleListView extends Activity {
private ListView lv1 = null;
private ListView lv2 = null;
private String s1[] = {"a", "b", "c", "d", "f", "g","h","i"};
private String s2[] = {"j", "k", "l", "m", "n", "o","p","q"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);         
    lv1 = (ListView) findViewById (R.id.list1);
    lv2 = (ListView) findViewById (R.id.list2);

    lv1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s1));
    lv2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s2));

}  

} 

这里我想列出内容,即 s1 和 s2 使用不同的语言而不是 a,b,c ..
我怎样才能做到这一点?

谢谢

编辑:

根据 android 中的语言支持 我开始知道我可以为 TextView 设置不同的语言。现在我想为列表内容设置语言。

I have two simple ListViews:

public class SimpleListView extends Activity {
private ListView lv1 = null;
private ListView lv2 = null;
private String s1[] = {"a", "b", "c", "d", "f", "g","h","i"};
private String s2[] = {"j", "k", "l", "m", "n", "o","p","q"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);         
    lv1 = (ListView) findViewById (R.id.list1);
    lv2 = (ListView) findViewById (R.id.list2);

    lv1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s1));
    lv2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s2));

}  

} 

Here I want to list-contents i.e. s1 and s2 to be of different language instead of a,b,c..
How can I do that?

Thanks

EDIT:

According to language support in android I came to know that I can set different language to the TextViews.Now I want to set the language to the list-contents.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从来不烧饼 2024-12-14 16:17:32

如果您想知道设备上的活动语言是什么,请查看此处 获取设备当前语言
然后根据语言填写你的s1和s2

Well if you're asking how to find out what the active language on the device is look here Get the current language in device
then fill your s1 and s2 according to the language

小兔几 2024-12-14 16:17:32

您应该使用本地化的 strings.xml 文件。

您的 res/values 文件夹包含一个 strings.xml 文件。

通过使用翻译后的 strings.xml 文件创建其他值文件夹,您可以涵盖所需的所有区域语言。

以下是一些示例:

  • res/values(针对英语)
  • res/values-de(针对德语)
  • res/values-zh(针对中文) )
  • res/values-es(西班牙语)
  • res/values-no(挪威语)
  • res/values-da(丹麦语)

全部该文件夹必须包含一个 strings.xml 文件翻译后的字符串。如果您的原始 strings.xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">English title</string>
</resources>

... 那么德语版本可能如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">German title</string>
</resources>

使用字符串资源

如果您想从字符串资源中获取字符串,请按以下方法操作你做。

在代码中: getString(R.strings.app_name) // 对于英语用户返回“English title”,对于德国用户返回“German title”,等等。

在布局: @string/app_name

更多信息

Android 开发者网站在此处提供了关于该主题的出色指南:

http://developer.android.com/guide/topics/resources/localization.html

You should use localized strings.xml files.

Your res/values folder contains a strings.xml file.

By creating additional values folders with a translated strings.xml file, you can cover all the regional languages you want.

Here are some examples:

  • res/values (for English)
  • res/values-de (for German)
  • res/values-zh (for Chinese)
  • res/values-es (for Spanish)
  • res/values-no (for Norwegian)
  • res/values-da (for Danish)

All the folder must contain a strings.xml file with the translated strings. If your original strings.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">English title</string>
</resources>

... then it could be like this for the German version:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">German title</string>
</resources>

Using string resources

If you want to get a string from the strings resources, then here's how you do.

In code: getString(R.strings.app_name) // returns "English title" for English users, "German title" for German users, etc.

In layout: @string/app_name

More info

The Android developer site has an excellent guide on the subject here:

http://developer.android.com/guide/topics/resources/localization.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文