Android学习笔记之二-----HelloWorld

本文从创建第一个Android项目开始,详细介绍了如何实现经典的HelloWorld应用。包括项目配置、资源引用及布局文件解析等内容。

最近,打算学习android,为了加深自己的理解。所以打算把学习历程记录下来。

因为之前已安装过环境,所以打算android环境安装过程以后再写一篇博客。1任何一门语言,开始的例子怎么少得了HelloWorld呢。所以,我第一个学习例子肯定就是Hello

World啦。

首先,新建一个android application project,然后一直next,最终finish保存即可

新建好项目之后,打开MainActivity.java,右键Run As ->Run Configuration是,出来以下图片:
 

点击Browse,选中自己新建的项目,然后在Launch中选择MainActivity这个类,运行即可。(第一次启动可能有点慢)。运行完之后出现以下图片:

这样,一个HelloWorld的程序就弄好了。刚开始可能会让人觉得很奇怪,感觉自己一行代码都没写,helloworld怎么就出来了。接下来就来分析以下。

打开AndroidManifest.xml这个文件,

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.android_001_helloworld.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>
    </application>

</manifest>

会看到有@string,@style,@drawable这样的写法。@string就对应/res/values下的strings.xml文件,

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Android_001_HelloWorld</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
</resources>
@string/app_name的值就是Android_001_HelloWorld。res目录写还有很多文件夹如下图:


这些xml文件最终会变异成一个类,R.java,


打开这个类会发现之前的layout,menu,string,style都会在里面建立相应的类和变量。而helloworld就是在res/layout下的activity_main.xml布局文件定义的,

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
而最终项目的运行是在MainActivity.java这个类来加载的。

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

public class MainActivity extends Activity {

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

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

在onCreate方法里,加载页面布局文件,activity_main.xml,而我们之前提到的syule.xml,string.xml,activity.xml中的属性都会在R.java里面生成类和变量,这一步不需要自己完成,应用程序编译完就会加载生成R.java。到这里Helloworld就结束了。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值