【问题标题】:Eclipse Android Development - [I18N] Hardcoded string "TextView", should use @string resourceEclipse Android 开发 - [I18N] 硬编码字符串“TextView”,应使用 @string 资源
【发布时间】:2014-10-02 23:33:03
【问题描述】:

我是 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" />

这里是突然出现的其他错误的截图: ![在此处输入图片说明][1]

【问题讨论】:

    标签: java android xml eclipse sdk


    【解决方案1】:

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

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

    下面是一个示例:
    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

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

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

    【讨论】:

    • 谢谢。这有帮助。我还想出了如何通过使用 android:inputType="text" 摆脱 问题
    • @neoraj3.0 没问题!
    【解决方案2】:

    硬编码字符串、缺少内容描述等不是错误,但Lint 警告 与您的应用程序的可用性有关。请咨询lint docs 了解更多信息。

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

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

    【讨论】:

    • 谢谢。我将查看 Lint 文档,因为我不熟悉它。
    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多