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

本文详细介绍了Android HelloWorld项目的创建过程及核心代码实现。从Activity的生命周期方法onCreate()入手,解析了如何加载并显示布局文件main.xml,展示了TextView组件的使用,并深入探讨了AndroidManifest.xml中的配置细节。
当大家看到HelloWorld的后我们会想到到底是怎么一个过程呢?

展开项目:

1.如图:

[img]http://dl.iteye.com/upload/attachment/377443/392e9c3d-3fc3-33d5-8477-74a30d3f2b12.jpg[/img]

先看源码:

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类

*前端
*后台服务
*间隔执行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值