Android開發學習筆記(二)我的第一個Android應用

來源:互聯網
上載者:User

好的,開門見山,我的第一個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後顯示出來的設定按鈕。

下面是我的最終結果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.