res 文件夹和 R.java 的问题

发布于 2024-10-24 09:35:50 字数 886 浏览 1 评论 0原文

我正在做教程,我在关于图像的部分。它说将它们放入文件夹 res/drawable 中。但我没有那个文件夹,我有三个: res/drawable-hdpi、res/drawable-ldpires/drawable-mdpi< /代码>。那么它们之间有什么区别呢?

我正在使用这个教程。

步骤之一是:

创建一个 strings.xml 文件 res/values/ 并编辑该文件以查看 喜欢

已经有strings.xml,结合上面的,告诉我使用res/drawable,这些教程已经过时了吗?

本教程的代码如下:

R.id.spinner
R.array.planets_array

R.layout 只是简单的枚举。使用布局文件夹中的 main.xml。但是 R.id 和 R.array 是从哪里来的呢?因为它在日食中出现,说它不知道它们是什么。 R.java 会自动更新,所以有人可以通过阅读该教程告诉我 id 添加到 R 的位置吗?它说

R.array.planets_array ID 引用定义的字符串数组 上面

只是它不起作用。我怀疑我没有创建 strings.xml 有什么区别,因为它在同一位置具有相同的文件名。但由于 R.java 是要自动更新的,我不知道如何解决这个问题。

I'm doing Tutorials and I'm on section about images. It says to put them into the folder res/drawable. But I don't have that folder, I have three instead: res/drawable-hdpi, res/drawable-ldpi and res/drawable-mdpi. So whats the difference between them?

Im using this tutorial.

One of the steps is:

Create a strings.xml file in
res/values/ and edit the file to look
like

There already is strings.xml, combined with the above, telling me to use res/drawable, are these tutorials out of date?

This tutorial has code like:

R.id.spinner
R.array.planets_array

R.layout is just simple enum. Uses the main.xml in the layout folder. But where are R.id and R.array to come from. Because it is coming up in eclipse saying it doesn't know what they are. R.java gets updated automatically, so can someone tell me from reading that tutorial where id gets added to R? It says that

The R.array.planets_array ID
references the string-array defined
above

Only it doesn't work. I doubt it makes a difference that i didn't make strings.xml since it's the same filename in the same location. But since R.java is meant to be updated automatically I don't know how to fix this.

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

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

发布评论

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

评论(3

只有影子陪我不离不弃 2024-10-31 09:35:50

这些适用于各种设备的不同屏幕分辨率。在 Android 开发网站上了解支持多个屏幕

Those are for the different screen resolutions for the range of devices that are out there. Read about supporting multiple screens on the Android dev site.

汐鸠 2024-10-31 09:35:50

只是为了让你知道 R 的东西来自哪里。

R.java 文件是一个生成的文件,其中包含某种指向应用程序中资源的指针。它实际上是一个简单的整数,在Android内部资源管理系统中唯一标识资源。

R.string 标识符是从像这样的资源 XML 文件生成的。

<resources>
    <string name="test">This is a test string.</string>
</resources>

来自字符串数组 XML 文件的 R.array 标识符。

<resources>
    <string-array name="days_of_week">
        <item>Monday</item>
        <item>Tuesday</item>
        <item>Wednesday</item>
        <item>Thursday</item>
        <item>Friday</item>
        <item>Saturday</item>
        <item>Sunday</item>
    </string-array>
</resources>

您现在可以使用其标识符 R.id.days_of_week 访问该数组。

R.id 标识符有点特殊。

它们以两种方式生成。第一个是当您使用 @+id/... 语法在 XML 布局文件中定义 View 时。请注意 + 符号。

另一种方法是在资源 XML 文件中定义它们,例如字符串。

<resources>
    <item type="id" name="first" />
    <item type="id" name="second" />
</resources>

然后,您只需在布局 XML 文件中使用它们,例如 @id/first。请注意,在声明它之前,当您引用它时,不再有no + 符号。

在代码中,您可以像这样使用它,R.id.first

还有很多其他资源。我想向您指出应用程序资源文章,并让请务必查看资源类型子文章。

Just so you know where the R stuff comes from.

The R.java file is a generated file which contains some kind of pointers to a resource in your application. It is a simple integer actually which uniquely identifies the resource in the internal resource management system of Android.

R.string identifiers are generated from resources XML files like this one for example.

<resources>
    <string name="test">This is a test string.</string>
</resources>

R.array identifiers from string array XML files.

<resources>
    <string-array name="days_of_week">
        <item>Monday</item>
        <item>Tuesday</item>
        <item>Wednesday</item>
        <item>Thursday</item>
        <item>Friday</item>
        <item>Saturday</item>
        <item>Sunday</item>
    </string-array>
</resources>

You can access that array using its identifier R.id.days_of_week now.

R.id identifiers are a bit special.

They are generated in two ways. The first one is when you define a View in your XML layout file using the @+id/... syntax. Note the + sign.

The other way is to define them in resource XML files like strings for example.

<resources>
    <item type="id" name="first" />
    <item type="id" name="second" />
</resources>

You'd then just use them in a layout XML file like this @id/first. Note that there is no + sign anymore as you reference it, before you were declaring it.

In code you then use it like this, R.id.first.

There are a lot of other resources. I'd like to point you to the Application Resources article and also make sure to checkout the Resource Types sub-article.

若言繁花未落 2024-10-31 09:35:50

如果您没有该文件夹,只需创建它。它基本上是在更具体的文件夹(如 res/drawable-hdpi)中没有资源的情况下的后备方案

*-xx 文件夹允许您为各种屏幕分辨率提供更具体的可绘制对象(图像)。

同样的原则适用于values/和values-xx/,其中xx是国家/地区代码; xx 版本允许您翻译 UI 消息。

If you don't have the folder, just create it. It is basically the fallback for the case that you don't have a resource in a more specific folder like res/drawable-hdpi

The *-xx folders allow you to provide more specific drawables (images) for various screen resolutions.

The same principle applies to values/ and values-xx/ where xx is a country code ; the xx versions allow you to have translations for UI messages.

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