硬codeD字符串" 3行为",应使用@string资源字符串、行为、资源、codeD

2023-09-12 07:06:12 作者:情歌唱给自己听

我是个初学者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的黄线:文本=黄色 根据2)黄线 的android:文本=排四 三角警告说 [国际化]硬codeD字符串黄,应使用@string资源 和同为warnings.Any暗示的休息吗?

And, I noticed : 1) yellow line under android:text="Yellow" 2) yellow line under android:text="row four" the Triangle warn says [I18N] Hardcoded string "Yellow", should use @string resource " and same for the rest of the warnings.Any suggestion?

推荐答案

这不是很好的做法,很难code字符串到您的布局文件。你应该将它们添加到字符串资源文件,然后从你的布局引用它们。

It is not good practice to hard code strings into your layout files. You should add them to a string resource file and then reference them from your layout.

这可以让你只需要编辑您的strings.xml文件更新,同时在所有布局的词黄的每一次出现。

This allows you to update every occurrence of the word "Yellow" in all layouts at the same time by just editing your strings.xml file.

有也为支持多种语言作为单独的strings.xml文件可用于为每个支持的语言极为有用。

It is also extremely useful for supporting multiple languages as a separate strings.xml file can be used for each supported language.

例如: 保存在水库/价值/ strings.xml中的XML文件:

example: XML file saved at res/values/strings.xml:

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

此布局XML字符串适用于查看:

This layout XML applies a string to a View:

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

同样颜色应储存在colors.xml然后引用通过使用@彩色/ color_name

Similarly colors should be stored in colors.xml and then referenced by using @color/color_name

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