android快速上手(二)android開發環境搭建及hello world

來源:互聯網
上載者:User

標籤:android   style   blog   class   code   java   

基本瞭解了java文法,下一步,我們一起開啟hello world的神秘之旅。


(一)android開發環境搭建

之前搭建android開發環境是件非常費力的事情,下載Eclipse,安裝ADT等,如今android官方給我們提供了全套配置。

https://developer.android.com/sdk/index.html

搭建android開發環境之前記得先安裝jdk


(二)開啟Hello World之旅

(1)建立Hello World項目

安裝完帶ADT的Eclipse,開啟Eclipse,建立新項目

一路next和finish下去,最後產生項目如下

不同版本建立的項目,產生的內容會不同,俺用的是4.4版本的SDK

運行項目


(2)項目結構分析

產生項目結構較為複雜,想深入瞭解的同學可以繼續看,也可以暫時略過。

1.drawable目錄存在圖片

android支援不同解析度手機圖片適配,對應圖片放在對應的檔案夾,圖片一般放於drawable-hdpi,圖片的xml放於drawable中

2.layout目錄存在布局

布局即顯示的UI,同樣支援不同的解析度和橫豎屏專門適配

3.values目錄存在數值資源資訊

color對應顏色值,string對應字串,dimens對應尺寸,styles對應主題樣式、menu存放菜單資訊等

4.AndroidManifest.xml檔案

聲明Activity、Service、Broadcast等資訊,設定app能使用的許可權、包名、版本資訊等

5.gen檔案夾

儲存自動產生的、位於android項目包下的R.java檔案,R檔案存放資源資訊映射值,程式中直接調用

6.libs檔案夾

存放第三方調用的lib


(3)hello wolrd深入

要使用控制項,需拿到控制項,拿控制項通過R中的控制項id值

在fragment_main.xml中添加helloworld文本的id值

<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="com.peter.demo.helloworld.MainActivity$PlaceholderFragment" >    <TextView        android:id="@+id/hello_world"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>


拿到TextView對象,對它進行顯示賦值

在MainActivity.java中
/**     * A placeholder fragment containing a simple view.     */    public static class PlaceholderFragment        extends Fragment {        public PlaceholderFragment() {        }        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {            View rootView = inflater.inflate(R.layout.fragment_main, container, false);            TextView tv = (TextView) rootView.findViewById(R.id.hello_world);            tv.setText("hello world!");            return rootView;        }    }

(4)helloworld擴充

完成了helloworld,下面讓我們一起繼續玩轉,千變萬化的Helloworld。


1.物件導向的helloworld

建立HelloWorld對象

/** * helloworld對象 *  * @author peter_wang * @create-time 2014-5-11 上午10:37:04 */public class HelloWorld {    private String mText;    public HelloWorld() {        this.mText = "Hello World!";    }    public String getmText() {        return mText;    }    public void setmText(String mText) {        this.mText = mText;    }}

修改MainActivity.java中TextView部分

TextView tv = (TextView) rootView.findViewById(R.id.hello_world);HelloWorld helloWorld = new HelloWorld();tv.setText(helloWorld.getmText());


2.修改Helloworld顯示樣式

TextView tv = (TextView) rootView.findViewById(R.id.hello_world);tv.setText("hello world!");//設定顯示顏色為綠色tv.setTextColor(Color.GREEN);//設定字型大小tv.setTextSize(18);//加粗TextPaint paint =  tv.getPaint();  paint.setFakeBoldText(true); 








(三)學習概要

開發環境搭建較容易,helloworld建立項目自動產生,熟悉下整個項目的結構,感興趣的同學自己發揮創意改下代碼,寫代碼一定要樂在其中,一切在自己的掌握中,建立自己的小東西。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.