【问题标题】:Multiple langues with textView and setText使用 textView 和 setText 的多种语言
【发布时间】:2013-01-03 02:10:34
【问题描述】:

我使用 ImageAdapter 设置带有图像和标题的自定义视图。如何支持此文本的多种语言?

文本现在在 imageAdapter 的 getView(...) 方法中是湿的:

public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.activity_main_icon, null);

            TextView tv = (TextView)v.findViewById(R.id.main_icon_text);
            tv.setText("**MENU TEXT**");

            ImageView iv = (ImageView)v.findViewById(R.id.main_icon_image);

            iv.setImageResource(mThumbIds[position]);
        } else {
            v = convertView;
        }

        return v;
    }

我猜“菜单文本”应该从 res/Strings 动态设置以支持不同的语言,但我该怎么做呢?

GridView 有四个图像,应该有四个不同的字符串。例如。 “添加好友”、“查找好友”、“编辑好友”和“删除好友”。

【问题讨论】:

    标签: android textview settext


    【解决方案1】:

    我建议您阅读此培训:

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

    然后,您的代码应该如下所示:

    view.setText(getResources().getString(R.string.YOURSTRINGKEY));

    【讨论】:

    • 是的,我明白了。问题是imageAdapter中的getView()对每张图片都会调用一次,所以每次必须使用不同的字符串。
    • 使用 ENUM 定义位置字符串,然后使用 Switch(position)
    【解决方案2】:

    先把字符串值放到一个资源文件中:res/values/strings.xml

    <string name="menu_text">menu text</string>
    

    然后在运行时解析字符串:

    String menuText = context.getString(R.string.menu_text);
    

    然后您可以使用字符串文件的外语版本进行翻译: 例如res/values-fr/strings.xml

    【讨论】:

    • 是的,问题是每个图像都会调用一次getView。但我可以用“位置”的 if 语句来解决这个问题。这是最好的方法吗?
    【解决方案3】:

    您只需使用TextView.setText(int) 变体(例如R.string.menu_text_hello)并输入strings into the respected res folders

    【讨论】:

    • 是的,所以我必须使用带有“位置”的 if 语句? IE。如果(位置==1)字符串= getString(R.string.menu1); else if(position == 2) getSTRing(R.string.menu_text2) etc..?
    • 嗯,你可以使用,比如 int text。并且在你的 if 和 elses 之后有 setText(text)。
    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多