如何从monodroid中的XML获取Button标题的数据?

时间:2013-08-22 12:07:04

标签: xamarin.android xamarin

我正在创建一个用户可以更改语言的应用。我正在考虑为每种支持语言创建一个xml文件,然后告诉应用程序根据用户选择的语言加载一个xml。例如,我有和xml文件名为English.xml,如下所示:

 <?xml version="1.0" encoding="UTF-8" ?>
 <English>
    <string name="LoginButtonText">Login</string>
 </English>

如何获取此LoginButtonText字符串以将其用作我的Button标题。 我正在使用Xamarin。知道我该怎么办?提前致谢。

2 个答案:

答案 0 :(得分:1)

如果您不想使用大多数Android应用程序遵循的标准strings.xml资源方法(http://developer.android.com/guide/topics/resources/string-resource.html#String),您可以执行以下操作:

像这样加载你的xml文档(你可能必须将文件的构建操作设置为“content”):

var stringsDoc = XDocument.Load ("English.xml");

从XML获取适当的字符串:

IEnumerable<XElement> strings =
    (from el in stringsDoc.Root.Elements("string")
    where (string) el.Attribute("name") == nameToLookFor
    select el);

var buttonLabel = strings.First ().Value;

在您的活动中,您可以设置按钮的标题,如下所示:

Button myButton = FindViewById<Button> (Resource.Id.button1);
myButton.Text = buttonLabel;

答案 1 :(得分:1)

在布局中使用以下代码,这将使语言根据所选语言从字符串文件动态加载

机器人:文本= “@串/ LoginButtonText”

您应该使用单独的字符串文件进行本地化。所以,为此,我为你附上了链接

https://developer.xamarin.com/guides/android/application_fundamentals/resources_in_android/part_5_-_application_localization_and_string_resources/

https://developer.xamarin.com/guides/android/advanced_topics/localization/

供我参考,我用它来做这个 英语:values - &gt; strings.xml 泰卢固语:values-te - &gt; strings.xml