硬编码字符串"第三行",应该使用@string资源

时间:2012-01-05 13:30:35

标签: android exception internationalization

我是初学Android开发者,我试图在eclipse中运行这个线性布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1">
      <TextView
          android:text="red"
          android:gravity="center_horizontal"
          android:background="#aa0000"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="green"
          android:gravity="center_horizontal"
          android:background="#00aa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="blue"
          android:gravity="center_horizontal"
          android:background="#0000aa"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="yellow"
          android:gravity="center_horizontal"
          android:background="#aaaa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
  </LinearLayout>

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextView
        android:text="row one"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row two"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row three"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row four"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
  </LinearLayout>

</LinearLayout>

并且,我注意到:
1) android:text="Yellow" 下的黄线 2) android:text="row four" 下的黄线 三角警告说[I18N] Hardcoded string "Yellow", should use @string resource " 其余的警告也一样。有什么建议吗?

5 个答案:

答案 0 :(得分:117)

将字符串硬编码到布局文件中并不是一种好习惯。您应该将它们添加到字符串资源文件中,然后从布局中引用它们。

这样,您只需编辑strings.xml文件,即可同时更新所有布局中每个“黄色”字样。

它对于支持多种语言也非常有用,因为可以为每种支持的语言使用单独的strings.xml文件。

例如: 保存在res / values / strings.xml的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="yellow">Yellow</string>
</resources>

此布局XML将字符串应用于视图:

<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/yellow" />

同样,颜色应存储在colors.xml中,然后使用@ color / color_name

进行引用
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="Black">#000000</color>
</resources>

答案 1 :(得分:11)

你必须在下面创建它们 的strings.xml

<string name="close">Close</string>    

你必须像这样替换和引用

android:text="@string/close"/>

即使XML文件显示strings.xml,也不要使用@strings,否则它将无效。

答案 2 :(得分:10)

  

将字符串硬编码到布局文件/代码中并不是一种好习惯。您应该将它们添加到字符串资源文件中,然后从布局中引用它们。

  1. 这允许您更新所有相同单词的每次出现 只需编辑strings.xml文件即可同时布局。
  2. 对于supporting multiple languages来说,它也非常有用 单独的strings.xml file可用于每种受支持的语言
  3. 拥有@string系统的实际点请阅读 localization文档。它允许您轻松找到文本 你的应用程序,然后将其翻译。
  4. 字符串可以轻松实现国际化,允许您的应用程序 到support multiple languages with a single application package file (APK)。
  5. <强>优势

    • 假设您在代码中的10个不同位置使用了相同的字符串。 如果你决定改变它怎么办?而不是搜索所有它 已在项目中使用,您只需更改一次并进行更改即可 反映在项目的各个角落。
    • 字符串不会使您的应用程序代码混乱,并保持清晰    易于维护。

答案 3 :(得分:3)

您可以进入设计模式并选择&#34;修复&#34;在警告的底部。然后会出现一个弹出窗口(看起来它会注册新的字符串)瞧,错误是固定的。

答案 4 :(得分:0)

一个好的做法是在String.xml中写入文本

示例:

String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="yellow">Yellow</string>
</resources>

和内部布局:

<TextView android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/yellow" />
相关问题