继续遇到错误,说我应该使用'@string'我在Android Studio上的XML资源

发布于 2025-01-27 16:16:50 字数 2813 浏览 3 评论 0原文

我正在使用Android Studio,我是新手。

对于某些对象(例如最后一个按钮对象),系统告诉我“硬编码的字符串'消息',应使用'@string'资源”。这是什么意思?

我已经在线搜索,有人说使用< resource>,但我不知道这意味着什么。我该如何解决?

例如,在第一个ImageView中,此处弹出一个错误Android:contentDescription =“ HomeImage”

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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="wrap_content"
    android:layout_height="match_parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity">

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="135dp"
            android:layout_height="130dp"
            android:layout_marginTop="36dp"
            android:layout_marginEnd="138dp"
            android:contentDescription="homeImage"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher_round"
            tools:ignore="ImageContrastCheck" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="150dp"
            android:layout_height="55dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="136dp"
            android:text="TextView"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2" />

        <Button
            android:id="@+id/followButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="28dp"
            android:layout_marginEnd="88dp"
            android:text="Follow"
            app:layout_constraintEnd_toStartOf="@+id/messageButton"
            app:layout_constraintTop_toBottomOf="@+id/textView" />

        <Button
            android:id="@+id/messageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="28dp"
            android:layout_marginEnd="72dp"
            android:text="Message"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

I'm using Android Studio and I'm new to this.

For some of the objects like the last button object, the system tells me "Hardcoded string 'Message', should use'@string' resource". What does this mean?

I've searched online and some say to use <resource> but I don't know what this means. How can I fix this?

For example, in the first ImageView, an error pops up here android:contentDescription="homeImage"

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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="wrap_content"
    android:layout_height="match_parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity">

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="135dp"
            android:layout_height="130dp"
            android:layout_marginTop="36dp"
            android:layout_marginEnd="138dp"
            android:contentDescription="homeImage"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher_round"
            tools:ignore="ImageContrastCheck" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="150dp"
            android:layout_height="55dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="136dp"
            android:text="TextView"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2" />

        <Button
            android:id="@+id/followButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="28dp"
            android:layout_marginEnd="88dp"
            android:text="Follow"
            app:layout_constraintEnd_toStartOf="@+id/messageButton"
            app:layout_constraintTop_toBottomOf="@+id/textView" />

        <Button
            android:id="@+id/messageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="28dp"
            android:layout_marginEnd="72dp"
            android:text="Message"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玉环 2025-02-03 16:16:50

这不是错误。
只是为了使重复使用字符串和翻译更容易

It's warning not an error.
And only to make it easier to reuse strings and also for translation

画▽骨i 2025-02-03 16:16:50

这是一个警告,告诉您您最好使用@String XML文件来包含您的文本。

在Android Studio中,资源是您应用所需的本地化文本字符串,位图,布局或其他一小部分非编码信息。在构建时间,所有资源都将汇编为您的应用程序。

例如,您在上面介绍的布局是一个资源文件,该文件包含在@layout文件夹中,您也可以使用@Drawable文件夹来保存图像向量...
Android Studio提供了许多其他文件夹,以控制您,并使您和系统更容易。
您一开始可能会很难,但是一段时间后,您会喜欢它。

有关更多信息,请查看 app app resources概述

It is a warning that tells you that you would better use the @string xml file to have your texts at .

In Android studio, a resource is a localized text string, bitmap, layout, or other small piece of noncoded information that your app needs. At build time, all resources get compiled into your application.

for example your layout you are presenting above is a resource file that goes in @layout folder you also can use @drawable folder to save your images vectors ...
and there is plenty of other folders android studio provide to control your and to make it easier for you and for the system .
you might at the beginning find it hard but after some time you will like it .

for more information ,have a look at App resources overview

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文