简体   繁体   English

为什么要求我在xml中将我的String用作@string引用?

[英]Why am I being asked to make my String in xml as @string reference?

@ string /参考错误

android:text="Button" doesn't work. android:text="Button"不起作用。 I should make a special string for it in the values/strings.xml and change the android:text="Button" to android:text="@string/button" for button having the value "Button" . 我应该在values/strings.xml为其创建一个特殊的字符串,并将具有values/strings.xml android:text="Button"android:text="Button"更改为android:text="@string/button" "Button"

What's the problem it used to work before? 它以前工作过的问题是什么?

Those are not error only warning, there is tool in every ide called lint which helps programmer to write code in standard format of that language. 这些不仅是错误警告,在每个想法中都有称为lint的工具,可以帮助程序员以该语言的标准格式编写代码。 In android standard practice is to use string.xml for storing all of your string value that is used in xml, so lint shows a warning that you should not directly set label hard-coded rather add them in string resource then set the resource id. 在android中,标准做法是使用string.xml来存储xml中使用的所有字符串值,因此lint显示警告,您不应直接设置硬编码标签,而应将其添加到字符串资源中,然后设置资源ID。

And there is some advantage of using string values like 并且使用诸如

  • If you want to give multi language support of your app then can use multiple string resources for this. 如果您想为您的应用提供多语言支持,则可以为此使用多个字符串资源。
  • Can be changed any time without changing source code. 可以随时更改而无需更改源代码。
  • All resource are added in R.java automatically so you can use them in class also. 所有资源都会自动添加到R.java中,因此您也可以在类中使用它们。

etc 等等

You can find more information here -- 您可以在这里找到更多信息 -

String Resources 字符串资源

hardcoded string "row three", should use @string resource 硬编码的字符串“第3行”,应使用@string资源

this answered my question, thank you, but if you have anything to add i'm glad to know it from you guys. 这回答了我的问题,谢谢,但是如果您有什么要补充的话,我很高兴收到你们的来信。 Best. 最好。

Alan 艾伦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在String中收集的xml输出格式不正确? - Why my xml output which I am collecting in String is not coming in proper format? Jakarta EE 10:为什么我在登录后被要求再次登录? - Jakarta EE 10: Why am I being asked to login again after already loggin in? 给定一个包含文件名的字符串,为什么我不能在Java中拆分一个字符串? - Why am I not being able to split a String in java given that I have a string containing filename? 为什么我无法将对象映射到此字符串 - Why am I unable to map my object to this String 为什么在压缩和解压缩后没有得到原始字符串? - Why am I not getting my original string after compression and decompression? 为什么我的Android应用程序上出现空字符串? - Why am I getting empty string on my android application? 为什么我不能直接引用我的String枚举? - Why can't I directly reference my String enum? 我不确定为什么我的班级没有返回正确的类型。 数组列表 <HashMap<String,String> &gt; - I am not sure why my class isn't returning the correct type. ArrayList<HashMap<String,String>> 为什么String是字符数组,但为什么不能作为“按引用传递” - Why does String not work as “pass by reference” in spite of being an array of characters 为什么我没有得到字符串格式的输出? - Why am I not getting the output in the string format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM