【问题标题】:Using the android:text="@string" does not replace string value in the strings.xml file使用 android:text="@string" 不会替换 strings.xml 文件中的字符串值
【发布时间】:2018-07-23 07:32:01
【问题描述】:

我是 Android 开发的新手。我最近下载了最新的 Android Studio 3.0.1。我正在使用华莱士杰克逊“绝对初学者的 Android 应用程序”。根据本书创建(第一个)应用程序时,我有 2 个 XML 文件:

  1. activity_main.xml
  2. strings.xml

activity_main.xml 文件硬编码了android:txt="Hello World!" 本书指导您修改文件 1 以引用 strings.xml 获取此字符串。 文件 2,strings.xml,被修改为添加第二行,其中包含通过删除“!”稍微修改的字符串。

当我查看 Android SDK 中的预览布局窗格时,我没有看到字符串“Hello World”

相反,我看到了“@string”。

我确定我做的事情基本上是错误的,或者错过了一个或多个关键步骤。

这是 activity_main.xml 文件(文件 1)

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.michael.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

这是 strings.xml 文件(文件 2):

    <resources>
        <string name="app_name">My Application</string>
        <string name="app_message">Hello WORLD</string>
    </resources>

【问题讨论】:

    标签: android xml string


    【解决方案1】:

    你必须指定字符串名称

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_message"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

    要访问 android 上的资源,您必须提供其资源 ID 才能访问它。

    见: https://developer.android.com/guide/topics/resources/accessing-resources.html

    【讨论】:

      【解决方案2】:

      访问字符串资源,如android:text="@string/string_name"

      string.xml

      <string name="string_name">hi this is my string</string>
      

      【讨论】:

        【解决方案3】:

        在你的strings.xml:

        <string name="app_message">Hello WORLD</string>
        

        在您的布局中:

        <?xml version="1.0" encoding="utf-8"?>
            <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.example.michael.myapplication.MainActivity">
        
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_message"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        
            </android.support.constraint.ConstraintLayout>
        

        【讨论】:

          猜你喜欢
          • 2011-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-16
          • 2020-02-26
          • 2021-11-18
          • 2019-03-13
          相关资源
          最近更新 更多