android - 为什么它告诉我在我使用 android :text ="' ? 的任何地方使用 @string/而且它是硬编码的

标签 android textview android-resources hardcoded

错误说对于硬编码你必须使用@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" />

最佳答案

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

字符串 提供单个字符串的 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"

关于android - 为什么它告诉我在我使用 android :text ="' ? 的任何地方使用 @string/而且它是硬编码的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31163298/

相关文章:

Android:在 TextView 中设置内联 html 链接的样式

android - 我们可以在 Android 设备上以编程方式启用/禁用 USB 调试吗?

android - 没有加号的新资源 id 声明

android - 元素在 relativeLayout 中相互重叠

android - TextView 消失了,Button 可见

android - 如何修复 Android Studio 中的 "Android resource linking failed"错误?

android - 4K 的资源限定符(不是 -xxxxhdpi?)807dpi

android - 如何在 XML 的微调器中添加提示

android - 银行数据库的 Postdating 交易

android - 如何在 TextView 中围绕部分文本设置矩形虚线/虚线轮廓?