在前面我们已经成功搭建了android开发环境,接下来我们就用Android的Eclipse插件ADT来创建第一个android程序:HelloWorld。相信大家对HelloWorld这个名字不会陌生。 1.选择“文件”>“新建”>“Android Application Project”,打开“New Android Project”
这里注意Application Name开头一定要大写,注意Package name must have at least two identifiers(我就不翻了),只写helloworld是不行的。
next,next之后finish就行了
在这个过程中要设置Activity Name 和Layout Name,如图
关于activity,大家要学好它的生命周期。这里大家先这样理解:一个activity就是一个界面,当大家点击一个按钮到另一个界面时,就到了另一个activity。
点击Finish之后我们就完成了一个最简单的Android应用项目的创建,注意,到现在一句代码都木有写哦!
在“包资源管理器”中,展开HelloWorld,这里有许多目录,下面我就给大家解释一下它们各自的含义与作用。
1. src目录:源文件文件夹,各种代码的编写就是在这里完成的。
2. gen目录:自动生成的R资源索引类文件夹,这里的东西是不能改动的。
这里多说一句,建议大家每增加一个ID就将gen目录刷新一下,这样防止因为eclpse没有在gen目录自动生成而报错的麻烦,万一报错那就一定是最后一个没自动生成
3.Android2.3.3目录:Android SDK jar文件。
4.assets目录:资源文件夹(这里的资源是不会在gen目录中自动生成ID的)。
5.res目录:资源文件夹 1.drawable-.....是不同分辨率的图片文件所在地
2.layout是布局文件,默认只有一个,程序员根据自己程序的需要可以添加更多的布局文件。
打开activity_main.xml如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" //设置宽的类型-跟随父类,这里没有父类,就默认全屏
android:layout_height="match_parent" > //设置高的类型-跟随父类,这里没有父类,就默认全屏
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world" //引用hello_world字符串,即Hello world!
tools:context=".MainActivity" />
</RelativeLayout>
3.字符串:在values目录下的string.xml文件中。
打开string.xml如下
<resources> <string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string> //<string name="所加的字符串">运行时显示在界面上的内容</string> </resources> 好处:可以创建多个string.xml,每个string.xml用不同语言,以方便不同国家的用户使用。 6.AndroidManifest.xml十分重要,内容如下 <uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> //有了这句就确认了第一个打开的activity
</intent-filter>
</activity>
</application> //在此处加应用权限,如入网权限: <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest> 运行结果如图:
中间就是TextView控件了
|