接上篇~
<application>元素
我们以以下这个例子来讨论<application>元素
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
...
</application>
现在看起来可能有点奇怪,@drawable/icon 和 @string/app_name 是什么呢?当我们开发了一个基于android的应用,我们通常用许多XML去定义一些特殊的资源。
我们必须要有能区分这些定义在XML中的资源(比如说图片,国际化字符串)。这些资源储存在res/文件夹下。
我们用先前的标记去索引这些资源。 @指定了我们希望索引在别处的资源。接下来的字符串标记了我们希望索引到的资源类别,并直接映射到res/目录下
资源所在的文件夹或者文件中。最后的部分指定了资源的名字,比如在之前的例子中的图片名字叫icon,字符串的名字叫app_name。这里的图片名字是一个真实的文件名,我们
可以在res/drawable/文件夹下找到。字符串app_name定义在res/values/strings.xml文件里。这个文件里定义的所有字符串将被用在应用中。
NOTE:
在android中,资源处理是一个非常灵活的,同时也是非常复杂的东西。在这本书中,我出于2个原因去放弃使用这种方法。
1、在游戏开发中,这种处理方法对于需要完全控制资源的我们有过度(杀伤力)。
2、Android有一个修改在res/文件夹下资源的习惯,特别是图片(它称为drawables),而这些
修改又不是我们在游戏开发中所希望的。
在游戏开发中我唯一建议使用android的资源系统是——国际化字符串。
我们通常在游戏开发中使用assets/文件夹来储存资源,在此文件夹下我们的资源将不会被做任何修改。
<application>元素的意义想必现在已经非常清楚了。
icon属性指定了用与作为应用图标的图片,图片储存于res/drawable/文件夹下,这个图标将在android market中和
我们的设备上显示,它也所有在<application>元素中定义的activity的默认图标。
debuggable属性指定了我们的应用是否能够调试,在开发中,我们通常把它设置为true。当你把应用发布到android market上的时候,就需要把它设置为false。
如果你不把他设置为true,你将不能在Eclipse上调试此应用。
我们仅仅讨论了一小部分你可以指定的<application>元素的子集及其属性,然而这些子集和属性以及足够我们开发游戏所用,如果你希望了解更多的内容,你可以在
Android开发官方文档上找到全部的内容。
<application>元素包涵了所有应用组件的精确定义(包涵activity,services)而且也包涵了任何我们添加的库。
下一个是<activity>~
另附上原文:
The <application> Element
As in the case of the <manifest> element, let’s discuss the <application> element in the
form of an example:
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
...
</application>
Now this looks a little bit strange. What’s up with the @drawable/icon and
@string/app_name strings? When developing a standard Android application, we usually
write a lot of XML files, each defining a specific portion of our application. To be able to
fully define those portions, we must also be able to reference resources that are not
defined in the XML file, such as images or internationalized strings. These resources are
located in subfolders of the res/ folder, as discussed in Chapter 2 when we dissected
the Hello World project in Eclipse.
To reference resources, we use the preceding notation. The @ specifies that we want to
reference a resource defined elsewhere. The following string identifies the type of the
resource we want to reference, which directly maps to one of the folders or files in the
res/ directory. The final part specifies the name of the resource—in the preceding case
an image called icon and a string called app_name. In the case of the image, it’s the
actual filename we specify, as found in the res/drawable/ folder. Note that the image
name does not have a suffix like .png or .jpg. Android will infer that automatically based
on what’s in the res/drawable/ folder. The app_name string is defined in the
res/values/strings.xml file, a file where all the strings used by the application will be
stored. The name of the string was defined in the strings.xml file.
NOTE: Resource handling on Android is an extremely flexible but also complex thing. For this
book, I decided to skip most of it for two reasons: it’s utter overkill for game development and we
want to have full control over our resources. Android has the habit of modifying resources placed
in the res/ folder, especially images (called drawables). That’s something we do not want asgame developers. The only thing I‘d suggest using the Android resource system for in game
development is internationalizing strings. We won’t get into that in this book; instead we’ll use
the more game development–friendly assets/ folder, which leaves our resources untouched
and allows us to specify our own folder hierarchy.
The meaning of the attributes of the <application> element should become a bit clearer
now. The icon attribute specifies the image from the res/drawable/ folder to be used as
an icon for the application. This icon will be displayed in the Android Market as well as in
the application launcher on the device. It is also the default icon for all the activities we
define within the <application> element.
The label attribute specifies the string being displayed for our application in the
application launcher. In the preceding example, this references a string in the
res/values/string.xml file, which is what we specified when we created the Android
project in Eclipse. We could also set this to a raw string, such as My Super Awesome
Game. The label is also the default label for all the activities we define in the
<application> element. The label will be shown in their title bar of our application.
The debuggable attribute specifies whether our application can be debugged or not. For
development, we should usually set this to true. When you deploy your application to
the market, just switch it to false. If you don’t set this to true, you won’t be able to
debug the application in Eclipse.
We have only discussed a very small subset of the attributes you can specify for the
<application> element. However, these are sufficient for our game development needs.
If you want to know more, you can find the full documentation on the Android
Developers site.
The <application> element contains the definitions of all the application components,
including activities and services, as well as any additional libraries used.