读书人

第一章:初入Android大门(教程篇)(上)

发布时间: 2012-09-06 10:37:01 作者: rapoo

第一章:初入Android大门(教程篇)(下)
当大家看到HelloWorld的后我们会想到到底是怎么一个过程呢?

展开项目:

1.如图:



先看源码:

package HelloWorld.hello;import android.app.Activity;import android.os.Bundle;public class HelloWorld2 extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);    /**载入main.xml  */        setContentView(R.layout.main);    }}

主程序继承了Activity类重写onCreate()方法在setContentView()也就是Activity 显示布局main.xml,布局的xml文件路径res/layout/main.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"    > <!-- android:layout_width="fill_parent" android:layout_height="fill_parent"设置当前视图高度宽度为父视图所占的高度和宽度 android:orientation="vertical"它确定了LinearLayout的方向,其值可以为*vertical, 表示垂直布局*horizontal, 表示水平布局 --><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /></LinearLayout>

<TextView />是个组件就好比是我们lable一样,android:text="@string/hello"这个设置这个组件的初始值,我们转到res/values/strings.xml和gan目录下的R.java看看

   <?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, HelloWorld2!</string>    <string name="app_name">HelloWorld</string></resources>

package HelloWorld.hello;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;    }}


@string引用hello这个常数
package HelloWorld.hello;    public static final class string {        public static final int app_name=0x7f040001;        public static final int hello=0x7f040000;    }

就好比我找到string 这个对象访问hello这个属性一样而这个hello静态常量对应strings.xml这篇XML文件中<string name="hello">Hello World, HelloWorld2!</string>这个配置项。Hello World, HelloWorld2!就是我们看到的文字了。

每一个Android都有一个AndroidManifest.xml设置文件 里面包含有哪些Activity,Service和Receiver打开这篇xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="HelloWorld.hello"      android:versionCode="1"      android:versionName="1.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".HelloWorld2"                  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></manifest> 





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

其中设置了 intent-filteran droid:name="android.intent.category.LAUNCHER

指定Activity默认运行主要在Activity上.
Android应用程序分为3类

*前端
*后台服务
*间隔执行

读书人网 >Android

热点推荐