【问题标题】:Why is it telling me to use @string/ where ever i am using android:text="' ? And that it's hardcoded为什么它告诉我在我使用 android:text="' 的地方使用 @string/ ?而且它是硬编码的
【发布时间】:2015-07-01 13:51:16
【问题描述】:

错误表明,对于硬编码,您必须使用 @string/.. 仅使用 "android:text= "price" 有什么问题??

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginTop="16dp"
    android:text="PRICE" />

【问题讨论】:

    标签: android textview android-resources hardcoded


    【解决方案1】:

    字符串资源为您的应用程序提供带有可选文本样式和格式的文本字符串。三种类型的资源可以为您的应用程序提供字符串:

    字符串 提供单个字符串的 XML 资源。 字符串数组 提供字符串数组的 XML 资源。 数量字符串(复数) 携带不同字符串以进行复数的 XML 资源。 所有字符串都能够应用一些样式标记和格式参数。

    欲了解更多信息,请阅读here

    另外最好使用字符串资源,这样如果你必须改变文本你只改变一个变量

    所以在您的 res 文件夹中会有另一个名为 values 的文件夹,然后访问 strings.xml 文件,在该文件中您将放置一个字符串资源以在您的应用程序中使用,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">yourAppName</string>
        <string name="hello_world">Hello world!</string>
        <string name="action_settings">Settings</string>
    
        <string name="price">PRICE</string>
    
    
    
    </resources>
    

    然后在您的 xml 文件中,您将 android:text="PRICE" 更改为 android:text = "@string/price"

    【讨论】:

    • 那么我到底需要用我的代码更改什么?
    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2022-06-16
    • 1970-01-01
    • 2019-03-09
    • 2010-12-06
    • 2021-12-22
    相关资源
    最近更新 更多