3.HelloWord项目运行分析

      接下来我们就分析一下我们这个HelloWord项目时怎么运行起来的吧。

      首先我们打开AndroidManifest.xml文件,我们之前在介绍安卓的目录结构的时候已经说过,这个文件是用于注册整个项目的组件的。如下图所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.com">

    <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=".HelloWordActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

        其中<activity>标签包裹的内容表示的是对HelloWordActivity这个活动的注册,如果你写了一个活动没有的这里进行注册,那么该活动是无法使用的。

        其中intent-filter 里的两行代码非常重要,<action   android:name= "android.intent.action.MAIN" />    和<category android:name="android.intent.category.LAUNCHER"/>    表示HelloWorldActivity是 这个项目的主活动,在手机上点击应用图标,首先启动的就是这个活动。就相当于这个活动相当于你进入一个网站的首页。

       那么HelloWordActivity这个文件是什么呢?它就是我们之前已经说过的安卓四大组件中的“活动”了,活动是安卓应用程序的门面,凡是你在应用中能看到的东西,都是放在活动中的。打开HelloWordActivity,如下所示:

package example.com;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class HelloWordActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello_word_layout);
    }
}

       我们看到,HelloWordActivity继承AppCompatActivity,这是一种向下兼容的Activity,可以将Activity在各个系统版本增加的特性和功能最低兼容到Android2.1版本。Activity是安卓提供的一个活动的基类,你得活动必须继承Activity或者其子类才可以工作(AppCompatActivity是Activity的一个子类)。

    然后我们看到HelloWordActivity这个活动中只有一个方法onCreate(),这个方法是一个活动被创建时必须执行的方法。我们在这里并没有发现Helloword字样,那么展示出来的HelloWord字样是写在哪里的呢?

   其实Android程序的设计讲究的逻辑和视图分离,安卓中有专门有xml文件来写视图布局,看上图的方法setContentView,就是引用了一个叫hello_word_layout的视图布局文件。

   我们在res/layout中找到这个布局文件,打开:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HelloWordActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

      现在还看不懂?没关系,后面我会对布局进行详细讲解的,你现在只需要看到上面代码中有一 个TextView,这是Android系统提供的一个控件,用于在布局中显示文字的。然后你终于在 TextView中看到了Hello    World!的字样!哈哈!终于找到了,原来就是通过 android:text="Hello    World!"    这句代码定义的。 

 

总结起来就是:

活动引用布局,然后注册到AndroidManifest.xml中

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值