好的,開門見山,我的第一個Android應用開始嘍!
開啟Eclipse,依次選擇File->New->other, 選擇Android Application Project,Next。然後就會出現下面的對話方塊。
需要填寫的依次是應用程式名稱,項目名,以及包名(按Java的包名來寫),以上配圖就是我填寫的。其它的都按預設配置。
下一頁依舊是預設。Next。
下一頁就到了選擇表徵圖icon的時候了,系統會為應用自動產生大中小高清四種不同的icon。
Next,選擇建立一個BlankActivity。名字預設,一路預設下去。
然後,一個超簡單的應用就誕生了。
系統自動為我產生了以下檔案樹。
下面我簡單介紹一下重要的的檔案的功能。
Hello 項目名
|-res 存放Android應用所用的全部資源,包括圖片,字串,顏色,尺寸等
|-values
|-strings.xml 存放字串資源
|-layout 存放介面布局檔案
|-activity_main.xml
|-drawable-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi
|-src
|-com.xujin.hello
|-MainActivity.java
|-gen
|-R.java 自動產生的檔案,不可更改
|-AndroidManifest.xml
Android項目的系統資訊清單檔,控制Android應用的名稱,表徵圖,存取權限等整體屬性。
以下是我的第一個應用程式的清單:
布局檔案 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" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="@string/hello_world" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello" /></RelativeLayout>
這是一個相關性布局RelativeLayout,其中有兩個文本,TextView代表一種文字框。
以下是MainActivity.java檔案,
package com.xujin.hello;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.activity_main, menu);return true;}}
這個檔案的作用是,
1)在應用oncreat的時候設定setContentView(R.layout.activity_main);使用activity_main.xml作為布局檔案
2)設定setting,就是手機上按下menu後顯示出來的設定按鈕。
下面是我的最終結果: