rt

首先 :  当一个工程部署到模拟环境上后,当用户点击你的应用名字“android:label="@string/app_name">”的时候,操作系统会根据配置清单文件(AndroidManifest.xml)寻找到下面的配置节点“<activity android:name=".HelloWorldActivity”这个activity;android:icon="@drawable/icon" 这个是应用显示的图标 这个你可以修改,自己找一个png格式的图片,拷贝到drawable-hdpi文件夹下,你会在R.java中自动添加这个图片的名字的属性,然后在这里修改icon为新生成的图片的名字就行了。

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloWorldActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

    这个配置节点下的activity节点,这个节点中的

<intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

     这两个配置,告诉操作系统,这个activity是程序运行的入口类,相当与main方法;在一个应用中只要有一个应用配这个属性就行了;

然后 :我们看看被指定为程序入口的这个activity的代码:

package com.android.activity;

import android.app.Activity;
import android.os.Bundle;

public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

    android平台实例化这个activity类,其中的onCreate这个方法不是由程序员来调用的,是由android这个平台来调用  的,这个类被实例化后就直接调用这个方法了。其中的super调用的父类的方法,是用来画出整个应用的界面的;

值得一提的是

setContentView(R.layout.main);

这端代码:android的设计模式是遵守mvc设计模式的,c这层就是activity层,v层是xml文件,在工程目录中的res下的layout目录下的文件,布局文件,m业务层,要自己写,不要和c层放到一起,这样代码的质量很差;那我们回到这句代码,我们现在知道activity相当与action了,那么在struts中负责跳转页面的代码是mapping.forword("xxxx");我想大家都知道,那么类比,在activity中,负责跳转的就是setContentview(xxx);括号中的内容就是视图层的引用。那我们再来看看这个引用的东西

R.layout.main

我前面的文章说过,R.java文件是用来引用资源的,那么看看这里我们就知道他引用了谁。对了,就是loayout目录下的main.xml;我们可以看R.java文件中的代码

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.android.activity;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

 

public static final class layout {
        public static final int main=0x7f030000;
    }

这里就是刚才调用的东西,最终会对应工程目录下的res下的layout下的main。xml文件,如果我们在layout下再建立一个androidxml类型的文件,保存后,会在R.java中自动生成对应的索引;

再然后

说道main.xml文件,我们看看这个文件,以往我们都是用html或者jsp来显示页面,现在用xml了,看看有什么不同,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>


 严格遵守xml文件的格式,

 其中,标签


LinearLayout


直接翻译中文是线性布局,那就是一种布局方式了,swing中也有,就是指在这个标签中的元素每个元素占一行。还有绝对布局;不建议使用,因为不知道使用者的手机屏幕大小。还有相对布局:常用;


TextView


相当于label标签。

再来仔细看看,标签中的属性

 


<LinearLayout 
<!--这个是标准的格式-->
    xmlns:android="http://schemas.android.com/apk/res/android"
<!-- 布局方式 由上到下垂直显示-->
    android:orientation="vertical"
<!--应用界面的宽:填充父面板 还有个取值"wrap_content" 是根据内容的长度来决定界面大小-->
    android:layout_width="fill_parent"
<!--应用界面的高:填充父面板 对于这个应用其父面板就是整个屏幕-->
    android:layout_height="fill_parent"
    >
<TextView  
<!--定制这个lable的宽度:父界面的宽度-->
    android:layout_width="fill_parent" 
<!--定制这个lable的高度:和内容的大小相同-->
    android:layout_height="wrap_content" 
<!--lable的内容:@代表访问R.java文件,string是R.java文件中的一个元素。/就是访问符号
hello是具体那个资源 这样就调用了文字 这些文字的信息在values文件夹下的string.xml文件中定义
 这样做是为了国际化 这个文件夹下不是只能有stirng.xml这一个文件存在,可以有很多xml文件
所以我们可以把文字分门别类存放 有利于复用 减少资源占用-->
    android:text="@string/hello"
    />

过程基本就是这样了;