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

来源:互联网 发布:js post调用java方法 编辑:程序博客网 时间:2024/05/21 10:59

最近,打算学习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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic 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就结束了。







0 0
原创粉丝点击