【问题标题】:hardcoded string "row three", should use @string resource硬编码字符串“第三行”,应该使用@string 资源
【发布时间】:2012-02-03 07:51:28
【问题描述】:

我是一名初学者 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 " 其余的警告也一样。有什么建议吗?

【问题讨论】:

    标签: android exception internationalization


    【解决方案1】:

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

    这允许您通过编辑您的 strings.xml 文件同时更新所有布局中每个出现的单词“Yellow”。

    它对于支持多种语言也非常有用,因为单独的 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>
    

    【讨论】:

    • (超出范围)也,它看起来像一张桌子。可能是tablelayout会更合适。此外,不鼓励使用 pt 中的 textsize。 sp 更灵活。
    • 为什么是@string/... 而不是@strings/...?
    • 您正在引用单个字符串。为什么会是复数?
    • @Kuffs:如果应用程序是单语的,并且需要显示“请输入小于 100 的值”之类的小 Toast,我们是否仍应遵循 String Resource 方法?跨度>
    • 如果您将来决定添加一种新语言,我希望它使代码更清晰,并且您的所有字符串都在一个地方。不过,归根结底,这是建议而不是规则。
    【解决方案2】:

    您必须在下面创建它们 字符串.xml

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

    你必须像这样替换和引用

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

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

    【讨论】:

    • @CroiOS 很棒的英语
    【解决方案3】:

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

    1. 这允许您更新所有相同单词的每次出现
      只需编辑您的 strings.xml 文件即可同时进行布局。
    2. 对于supporting multiple languages 作为一个 单独的strings.xml file 可用于每种支持的语言
    3. 拥有@string系统的实际意义请阅读 localization 文档。它使您可以轻松地在 您的应用,然后翻译它。
    4. 字符串可以轻松国际化,允许您的应用程序 到support multiple languages with a single application package file (APK)。

    好处

    • 假设您在代码的 10 个不同位置使用了相同的字符串。 如果你决定改变它怎么办?而不是搜索它的全部位置 已在项目中使用,您只需更改一次即可 体现在项目的各个方面。
    • 字符串不会弄乱您的应用程序代码,保持清晰和 易于维护。

    【讨论】:

      【解决方案4】:

      您可以进入设计模式并选择警告底部的“修复”。然后会出现一个弹窗(好像要注册新的字符串),瞧,错误已修复。

      【讨论】:

        【解决方案5】:

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

        示例:

        字符串.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" />
        

        【讨论】:

          猜你喜欢
          • 2020-08-16
          • 1970-01-01
          • 1970-01-01
          • 2014-10-02
          • 1970-01-01
          • 2016-06-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多