【问题标题】:Hardcoded string "Test", should use @string resource硬编码字符串“Test”,应使用@string 资源
【发布时间】:2012-08-03 11:51:59
【问题描述】:

这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    
   
    <TextView
   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ref"    
     />
   
    <TextView
   
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Test"
     />
 
</LinearLayout>

我不想在第二个测试中使用字符串。我该怎么办?

【问题讨论】:

    标签: android-layout


    【解决方案1】:

    您是否使用硬编码的字符串没有问题。 如果您想了解更多信息,可以查看:https://stackoverflow.com/a/8743887/1517996

    字符串.xml

    <resources>
        <string name="Test">Test</string> 
    <resources>
    

    并像使用 lin 布局文件一样使用它

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

    【讨论】:

    • 我已经看到了那个链接,但我不希望我的代码有任何错误:/有什么办法可以避免这个错误?
    • 这不是错误,这是警告。如果你不想要这个警告,你应该在 strings.xml 中实现你的“Test”字符串作为 Test 并像 请看我编辑的帖子
    【解决方案2】:

    如果您不希望此警告专门针对此视图打扰您,您可以使用 tools:ignore xml 属性来抑制它。

    <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/Test"
      tools:ignore="HardcodedText" />
    

    请注意,您还可以将其添加到父元素以禁止对其子元素发出警告。

    【讨论】:

      【解决方案3】:

      您可以使用硬编码值,这很好。如果要删除警告,可以转到 Eclipse -> 首选项 -> Android -> Lint 错误检查并查找“硬编码文本”。将其设置为忽略然后应用。

      【讨论】:

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