Eclipse Android 开发 - [I18N] 硬编码字符串“TextView”,应使用 @string 资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25221250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:51:54  来源:igfitidea点击:

Eclipse Android Development - [I18N] Hardcoded string "TextView", should use @string resource

javaandroidxmleclipsesdk

提问by neoraj3.0

I am new to android developmentand tried creating a new layout named new_layout.xml which I actually saw in a Lynda training course with the code below and it worked fine. But every time I close off Eclipse and the AVD and start it up later I get some errors in the xml file:

我是 android 开发的新手,并尝试创建一个名为 new_layout.xml 的新布局,我实际上在 Lynda 培训课程中看到了下面的代码,它运行良好。但是每次我关闭 Eclipse 和 AVD 并稍后启动它时,我都会在 xml 文件中遇到一些错误:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />  <!-- [I18N] Hardcoded string "TextView", should use @string resource  -->

<EditText   <!-- This text field does not specify an inputType or a hint -->
    android:id="@+id/editText1"
    android:layout_width="wrap_content"   <!-- Use a layout_width of 0dp instead of wrap_content for better performance -->
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

Here is a screenshot of the other errors that suddenly appeared: ![enter image description here][1]

这是突然出现的其他错误的屏幕截图:![在此处输入图像描述][1]

回答by Matthieu Grieger

Eclipse is encouraging you to use the strings.xmlfile to declare strings instead of hard-coding them into other project files, like you are doing.

Eclipse 鼓励您使用该strings.xml文件来声明字符串,而不是像您现在所做的那样将它们硬编码到其他项目文件中。

The strings.xmlfile can be found in res/values/strings.xmlin your project. Simply place your strings in this file like shown in this example, and reference those strings in your other project files.

strings.xml文件可以res/values/strings.xml在您的项目中找到。只需将您的字符串放在这个文件中,如本例所示,并在您的其他项目文件中引用这些字符串。

Here's an example of what it might look like:
new_layout.xml

下面是它的外观示例:
new_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/textView" />  <!-- [I18N] Hardcoded string "TextView", should use @string resource  -->

<EditText   <!-- This text field does not specify an inputType or a hint -->
    android:id="@+id/editText1"
    android:layout_width="wrap_content"   <!-- Use a layout_width of 0dp instead of wrap_content for better performance -->
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button" />

res/values/strings.xml

res/values/strings.xml

<resources>
    <string name="textView">TextView</string>
    <string name="button">Button</string>
</resources>

Since the screenshot is not correctly linked, I only have the title to go off of, so hopefully this helps.

由于屏幕截图没有正确链接,我只有标题要关闭,所以希望这会有所帮助。

回答by Droidman

Hardcoded strings, missing content description and so on are no errors, but Lintwarningsrelated to usability of your application. Consult the lint docsto find out more.

硬编码字符串、缺少内容描述等都不是错误,而是与应用程序可用性相关的Lint警告。查阅lint 文档以了解更多信息。

Example: you can place the same String into /res/valuesand (for example) /res/values-defolders. If the device language is German, the system will take the string from the folder qualified with -de. Hardcoding a string means it will remain the same no matter of device settings.

示例:您可以将相同的字符串放入/res/values和(例如)/res/values-de文件夹中。如果设备语言为德语,系统将从以-de. 对字符串进行硬编码意味着无论设备设置如何,它都将保持不变。

It's not an error, but a matter of usability. Same applies to all Lintwarnings.

这不是错误,而是可用性问题。这同样适用于所有Lint警告。