Problems with res folder and 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
Th开发者_如何学Cere 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.
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.
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.
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.
精彩评论