继续遇到错误,说我应该使用'@string'我在Android Studio上的XML资源
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是错误。
只是为了使重复使用字符串和翻译更容易
It's warning not an error.
And only to make it easier to reuse strings and also for translation
这是一个警告,告诉您您最好使用@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