硬编码字符串"row three",应该使用@string资源

55 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>
Run Code Online (Sandbox Code Playgroud)

并且,我注意到:
1) 三角警告 下的黄线下面的黄线表示 并且其余的警告也是如此.任何建议?android:text="Yellow"
android:text="row four"
[I18N] Hardcoded string "Yellow", should use @string resource "

Kuf*_*ffs 121

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

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="yellow">Yellow</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

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

<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/yellow" />
Run Code Online (Sandbox Code Playgroud)

类似地,颜色应存储在colors.xml中,然后使用@ color/color_name引用

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="Black">#000000</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

  • (超出范围),它看起来像一个表格.可能是一个tablelayout会更合适.此外,沮丧地将文本化为pt.sp更灵活. (3认同)
  • 您正在引用单个字符串.为什么会是复数? (2认同)

小智 13

您必须在strings.xml下创建它们

<string name="close">Close</string>    
Run Code Online (Sandbox Code Playgroud)

你必须像这样替换和引用

android:text="@string/close"/>
Run Code Online (Sandbox Code Playgroud)

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

  • @CroiOS很棒的英语 (4认同)
  • 很好的解释 (2认同)

Xar*_*mer 10

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

  1. 这样,您
    只需编辑strings.xml文件即可同时更新所有布局中同一个单词的每个匹配项.
  2. 它也非常有用,supporting multiple languages因为strings.xml file可以将单独的语言用于每种受支持的语言
  3. @string系统的实际点请阅读 本地化文档.它允许您轻松地在应用程序中查找文本,然后将其翻译.
  4. 字符串可以轻松实现国际化,允许您的应用程序support multiple languages with a single application package file (APK).

优点

  • 假设您在代码中的10个不同位置使用了相同的字符串.如果你决定改变它怎么办?而不是搜索项目中所有它的使用位置,您只需更改一次,并在项目的任何位置反映更改.
  • 字符串不会使应用程序代码混乱,使其清晰易用.