android app(3)-Hello world程序分析

来源:互联网 发布:捕鱼游戏机核心算法 编辑:程序博客网 时间:2024/05/19 03:17

    上一节中使用向导生成了第一个程序"Hello world!"。

    这是一个JAVA程序,它运行在Dalvik虚拟机中,不同其它的虚拟机,每个Android应用程序都运行在单独的Dalvik虚拟机中。Dalvik运行的是.dex文件,并非JVM的.class文件。

    Hello world程序生成后,它的入口在MainActivity.java

    

package com.myexample.test0;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

MainActivity是程序创建的一个活动,它从Activetity继承。

Activetity是Android app中用户交互界面的组件,它是Android app界面开发中最重要的组件之一,类似于windows开发中的"窗体"。

java.lang.Object   ↳android.content.Context    ↳android.content.ContextWrapper     ↳android.view.ContextThemeWrapper      ↳android.app.Activity

Activetity的继承类一般会重载两个方法:

(1)onCreate(Bundle)

    它在Activetity创建时调用,在这里做UI界面的初始化工作。setContentView(R.layout.activity_main)的作用是设置UI界面为资源activity_main。

(2)onPause()

    它在Activetity离开时调用。

一个Activetity可以调用另一个Activetity:使用父类Contex的startActivity()方法,但它必须在AndroidManifest.xml中声明:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.myexample.test0"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" ><span style="color:#ff0000;">        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity></span>    </application></manifest>

每个资源都会生成一个id,保存R.java中,R.java是编译时自动生成的一个文件,如果要在程序中使用一个资源,例如:

setContentView(R.layout.activity_main);

资源文件在项目的res文件夹中:


drawable-xxxhdpi分别是高,低、中、超高、巨高分辨率的图片文件。

activity_main.xml是activity_main活动的界面布局文件:

<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"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>

TextView是一个文本框,它显示资源hello_word中定义的字体串。


strings.xml是字符串常量的定义:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Test0</string>    <string name="hello_world">Hello world!</string></resources>
如果要在xml中使用一个字符串,activity_main.xml有一个示例:

android:text="@string/hello_world"

styles.xml资源:

<resources>    <!--        Base application theme, dependent on API level. This theme is replaced        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.    -->    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">        <!--            Theme customizations available in newer API levels can go in            res/values-vXX/styles.xml, while customizations related to            backward-compatibility can go here.        -->    </style>    <!-- Application theme. -->    <style name="AppTheme" parent="AppBaseTheme">        <!-- All customizations that are NOT specific to a particular API-level can go here. -->    </style></resources>

另外还一些依赖库,这就是一个Android App的所有文件。

当生成一个app时,它生成的是一个安装文件,后缀是apk。在IDE启动调试或运行时,它会使用adb命令安装到目标机器。



0 0
原创粉丝点击