[Android]Hello, Android!

在Android平台刚刚推出的时候就对它非常关注,借intern的机会终于成为了一名Android apps developer。Java的基础和Android入门集中在最近的一两周,通过本周的实战任务(Menu编写)体会到一点点开发的感觉,简单地做一下回顾和记录,有待随着日后对于Android的理解加深而不断地修正补充。

一个Android project的目录架构长这样:

接下来对其中主要的一些catalogs进行介绍。

src: 和普通的java project类似,源程序文件都放在src文件目录下。

res: 这个目录应该是Android project的一个特色。drawable文件夹中存储了app所需要的图片文件,h/l/m-dpi代表了图片的分辨率。layout, values文件夹中存放了xml格式文件。layout定义了app每一个页面的布局(控件们如何摆放),values中存放的典型文件为strings.xml和arrays.xml.

strings.xml: 程序中的一些字符串常量

arrays.xml: 数组常量

AndroidManifest.xml: 目前还没有办法用术语来对这个文件进行介绍。根据个人的理解,定义了程序的入口,register了src中的各个class.

结合经典的"Hello, world!"例子对上面介绍的这些文件一窥究竟。

首先是res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">Hello World, HelloWorldActivity!</string>
<string name="app_name">HelloWorld</string>

</resources>

文件中定义了名为"hello"和"app_name"的字符串。使用时通过"@string/string_name"的形式引用。

布局文件res/layout/main.xml:

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

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

</LinearLayout>

定义了页面的整体布局为LinearLayout线性布局,并在布局上放置了一个TextView容器,以显示一行文字。每一个容器都有一些属性(attributes), 如大小,方向等等(每次定义的时候都会回顾一下android developer手册)。其中TextView的text属性引用了在res/values/strings.xml文件中定义的字符常量“@string/hello/”, 是一个strings.xml文件使用的实例。

src/HelloWorldActivity.class

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);
}
}

app的行为由一个个activity组成。onCreate函数创建一个Activity. setContentView函数定义了页面布局,使用/res/layout/main.xml中定义好的布局。

AndroidManifest.xml

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

<uses-sdk android:minSdkVersion="7"/>

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

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>

大部分内容都由系统自动生成。register了HelloActivity.class。如果project中包含了多个activity,则都需要通过

<activity 
android:label="@string/app_name_activityName"
android:name
=".className"/>

的形式在AndroidManifest.xml中定义。

<action android:name="android.intent.action.MAIN"/>

这句代码貌似起到了定义程序的主入口的作用。

如果需要对手机的硬件进行操作,则可能还需要在Android Manifest Permissions选项卡中进行设置。(今天背光调节的函数崩溃就是因为未在AndroidManifest.xml中进行Permissions设置导致的)

完成了以上文件的编写和设置,一个HelloWorldActivity project也就完成了。上载至VM或者手机即可看到运行结果。更复杂的app在上述的基础上包含了更多更复杂的文件。

原文地址:https://www.cnblogs.com/practice/p/2254771.html