hardcoded string “row three”, should use @string resource

前端 未结 5 1834
抹茶落季
抹茶落季 2020-11-30 18:39

I\'m a beginner android developer , I was trying to run this Linear Layout in eclipse :

             


        
相关标签:
5条回答
  • 2020-11-30 19:04

    You can go to Design mode and select "Fix" at the bottom of the warning. Then a pop up will appear (seems like it's going to register the new string) and voila, the error is fixed.

    0 讨论(0)
  • 2020-11-30 19:05

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

    1. This allows you to update every occurrence of the same word in all
      layouts at the same time by just editing your strings.xml file.
    2. It is also extremely useful for supporting multiple languages as a separate strings.xml file can be used for each supported language
    3. the actual point of having the @string system please read over the localization documentation. It allows you to easily locate text in your app and later have it translated.
    4. Strings can be internationalized easily, allowing your application to support multiple languages with a single application package file (APK).

    Benefits

    • Lets say you used same string in 10 different locations in the code. What if you decide to alter it? Instead of searching for where all it has been used in the project you just change it once and changes are reflected everywhere in the project.
    • Strings don’t clutter up your application code, leaving it clear and easy to maintain.
    0 讨论(0)
  • 2020-11-30 19:15

    A good practice is write text inside String.xml

    example:

    String.xml

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

    and inside layout:

    <TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/yellow" />
    
    0 讨论(0)
  • 2020-11-30 19:16

    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.

    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.

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

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

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

    This layout XML applies a string to a View:

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

    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>
    
    0 讨论(0)
  • 2020-11-30 19:20

    You must create them under strings.xml

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

    You must replace and reference like this

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

    Do not use @strings even though the XML file says strings.xml or else it will not work.

    0 讨论(0)
提交回复
热议问题