上一篇我们成功运行了Hello World,现在我们详细来看看项目结构和运行过程或者说运行流程。
项目结构
- AndroidManifest.xml
| 1234567891011121314151617181920212223 | <``manifest`` ``xmlns:android``=``"http://schemas.android.com/apk/res/android"`` ``package``=``"com.example.myapplication"``>`` ``<``application`` ``android:allowBackup``=``"true"`` ``android:icon``=``"@mipmap/ic_launcher"`` ``android:label``=``"@string/app_name"`` ``android:roundIcon``=``"@mipmap/ic_launcher_round"`` ``android:supportsRtl``=``"true"`` ``android:theme``=``"@style/AppTheme"``>`` ``<``activity`` ``android:name``=``".MainActivity"`` ``android:exported``=``"true"`` ``android:label``=``"@string/app_name"`` ``android:screenOrientation``=``"portrait"``>`` ``<``intent-filter``>`` ``<``action android:name``=``"android.intent.action.MAIN" ></``action``>`` ``<``category android:name``=``"android.intent.category.LAUNCHER" ></``category``>`` ``</``intent-filter``>`` ``</``activity``>`` ``</``application``>``</``manifest``> |
|---|
xmlns:android:定义android命名空间,这样使得Android中各种标准属性能在文件中使用,提供了大部分元素中的数据。
package:指定本应用内java主程序包的包名,它也是一个应用进程的默认名称。
application:一个AndroidManifest.xml中必须含有一个Application标签,这个标签声明了每一个应用程序的组件及其属性(如icon、label、permission等)。
allowBackup:当allowBackup标志为true时,用户即可通过adb backup和adb restore来进行对应用数据的备份和恢复,这可能会带来一定的安全风险。
icon:这个很简单,就是声明整个APP的图标,图片一般都放在drawable文件夹下。
roundIcon:同上,唯一区别它是圆形的图标,如果配置了此项,APP安装在Android 7.1.1以上的手机显示的就是圆形图标。
label:声明整个APP的名字,字符串常量一般都放在values文件夹下的strings.xml里。
supportsRtl:支持从右往左显示的布局(正常布局在镜子里面看到的左右对调过的样子)。
theme:是一个资源的风格,它定义了一个默认的主题风格给所有的activity,当然也可以在自己的theme里面去设置它,有点类似style。
activity:定义APP中的一个组件Activity。
name:该Activity的名字。
screenOrientation:该Activity的屏幕方向。
intent-filter:广播过滤器,后续会讲到。
action android:name:指定程序入口Activity,在这里是MainActivity。
category android:name:指定当前动作(Action)被执行的环境。这里的CATEGORY_LAUNCHER决定应用程序是否显示在程序列表里。
- java文件夹
该文件夹是放项目的源代码。我们来看MainActivity.java的源码
| 1234567 | public class MainActivity ``extends AppCompatActivity {`` ``@Override`` ``protected void onCreate(``@Nullable Bundle savedInstanceState) {`` ``super``.onCreate(savedInstanceState);`` ``setContentView(R.layout.act_main);`` ``}``} |
|---|
这里我们暂时只看第⑤行,这一句的意思就是将布局文件act_main设置为该Activity的视图,也就是说MainActivity打开后显示的页面就是act_main。
- res文件夹
程序资源目录。包含三个子目录:drawabel、layout、values。
drawabel:程序里所有的图片、形状。
layout:每个Activity界面对应的布局文件。我们来看看MainActivity的布局文件act_main.xml:
| 123456789101112 | <?``xml version``=``"1.0" encoding``=``"utf-8"``?>``<``LinearLayout xmlns:android``=``"http://schemas.android.com/apk/res/android"`` ``android:layout_width``=``"match_parent"`` ``android:layout_height``=``"match_parent"`` ``android:gravity``=``"center"``>`` ``<``TextView`` ``android:layout_width``=``"wrap_content"`` ``android:layout_height``=``"wrap_content"`` ``android:text``=``"Hello World" />``</``LinearLayout``> |
|---|
这里暂时只看第7行到第11行,可以理解为一个显示Hello World的标签。
values:程序中的文字strings.xml、大小dimens.xml、颜色colors.xml、风格styles.xml等。例如上面act_main布局文件中的Hello World文字就可以定义在strings.xml中,如下:
| 123 | <``resources``>`` ``<``string name``=``"text_hello"``>Hello World</``string``>``</``resources``> |
|---|
文字如果定义在了strings.xml中,那么布局文件中显示文字时使用@string/后面跟上文字对应的name来表示,如下:
| 1234 | <``TextView`` ``android:layout_width``=``"wrap_content"`` ``android:layout_height``=``"wrap_content"`` ``android:text``=``"@string/text_hello" /> |
|---|
再比如我们还可以设置文字的颜色为红色,我们可以这样设置,在colors.xml(如果没有可以新建一个文件)中定义:
| 1234 | <?``xml version``=``"1.0" encoding``=``"utf-8"``?>``<``resources``>`` ``<``color name``=``"tv_red"``>#ff0000</``color``>``</``resources``> |
|---|
然后在布局文件中设置文字的颜色为@color/后面跟上颜色对应的name,如下:
| 12345 | <``TextView`` ``android:layout_width``=``"wrap_content"`` ``android:layout_height``=``"wrap_content"`` ``android:text``=``"@string/text_hello"`` ``android:textColor``=``"@color/tv_red" /> |
|---|
- Gradle Scripts文件
这个文件夹下面都是项目编译和构建的一些配置,读者可以暂时跳过这些配置,下一篇章会着重解释。
运行过程/运行流程
Android程序会先去加载AndroidMainfest.xml文件,接着去找到application标签下intent-filter action android:name=”android.intent.action.MAIN”的Activity作为第一个Activity启动,启动后会先调用Activity的onCreate函数,而我们在此函数中通过setContentView设置了视图act_main,所以最终我们在手机屏幕上看到的是MainActivity页面上显示的Hello World文字。