Hello Android例子的內(nèi)部細(xì)節(jié)含代碼

    Hello Android例子的內(nèi)部細(xì)節(jié)

    在這里,我們將學(xué)習(xí)Hello Android例子的內(nèi)部細(xì)節(jié)或工作原理。愛掏網(wǎng) - it200.com

    Android應(yīng)用程序包含不同的組件,如Java源代碼,字符串資源,圖像,清單文件,APK文件等。愛掏網(wǎng) - it200.com讓我們來了解一下Android應(yīng)用程序的項(xiàng)目結(jié)構(gòu)。愛掏網(wǎng) - it200.com

    Java源代碼

    讓我們來看看由Eclipse IDE創(chuàng)建的Java源文件:

    package com.example.helloandroid;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.TextView;
    public class MainActivity extends Activity {//(1)
        @Override
        protected void onCreate(Bundle savedInstanceState) {//(2)
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);//(3)
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {//(4)
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }
    

    (1) Activity 是一個(gè)Java類,它在屏幕上創(chuàng)建并顯示一個(gè)默認(rèn)窗口,我們可以在窗口中放置不同的組件,例如按鈕、編輯框、文本視圖、下拉菜單等。愛掏網(wǎng) - it200.com它類似于Java AWT的框架。愛掏網(wǎng) - it200.com

    它為Activity提供了生命周期方法,例如onCreate、onStop、onResume等。愛掏網(wǎng) - it200.com

    (2) onCreate方法 在Activity類被首次創(chuàng)建時(shí)調(diào)用。愛掏網(wǎng) - it200.com

    (3) setContentView(R.layout.activity_main) 提供了關(guān)于我們布局資源的信息。愛掏網(wǎng) - it200.com在這里,我們的布局資源是在activity_main.xml文件中定義的。愛掏網(wǎng) - it200.com

    <RelativeLayout xmlns:androclass="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:layout_centerVertical="true"
            android:text="@string/hello_world" />
    </RelativeLayout>
    

    如您所見,一個(gè)TextView是由框架自動(dòng)創(chuàng)建的。愛掏網(wǎng) - it200.com但是這個(gè)字符串的消息是在strings.xml文件中定義的。愛掏網(wǎng) - it200.com @string/hello_world 提供有關(guān)TextView消息的信息。愛掏網(wǎng) - it200.comhello_world屬性的值是在strings.xml文件中定義的。愛掏網(wǎng) - it200.com

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">helloandroid</string>
        <string name="hello_world">Hello world!</string>
        <string name="menu_settings">Settings</string>
    </resources>
    

    您可以從此文件更改hello_world屬性的值。愛掏網(wǎng) - it200.com

    生成的R.java文件

    這是一個(gè)自動(dòng)生成的文件,包含了res目錄中所有資源的ID。愛掏網(wǎng) - it200.com它是由aapt(Android資源打包工具)生成的。愛掏網(wǎng) - it200.com每當(dāng)您在activity_main上創(chuàng)建任何組件時(shí),R.java文件中會(huì)創(chuàng)建相應(yīng)的ID,稍后可以在Java源文件中使用它。愛掏網(wǎng) - it200.com

    /* AUTO-GENERATED FILE.  DO NOT MODIFY.
     *
     * This class was automatically generated by the
     * aapt tool from the resource data it found.  It
     * should not be modified by hand.
     */
    package com.example.helloandroid;
    public final class R {
        public static final class attr {
        }
        public static final class drawable {
            public static final int ic_launcher=0x7f020000;
        }
        public static final class id {
            public static final int menu_settings=0x7f070000;
        }
        public static final class layout {
            public static final int activity_main=0x7f030000;
        }
        public static final class menu {
            public static final int activity_main=0x7f060000;
        }
        public static final class string {
            public static final int app_name=0x7f040000;
            public static final int hello_world=0x7f040001;
            public static final int menu_settings=0x7f040002;
        }
        public static final class style {
            /** 
            Base application theme, dependent on API level. This theme is replaced
            by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
                Theme customizations available in newer API levels can go in
                res/values-vXX/styles.xml, while customizations related to
                backward-compatibility can go here.
             Base application theme for API 11+. This theme completely replaces
            AppBaseTheme from res/values/styles.xml on API 11+ devices.
      API 11 theme customizations can go here. 
            Base application theme for API 14+. This theme completely replaces
            AppBaseTheme from BOTH res/values/styles.xml and
            res/values-v11/styles.xml on API 14+ devices.
     API 14 theme customizations can go here. 
             */
            public static final int AppBaseTheme=0x7f050000;
            /**  Application theme. 
     All customizations that are NOT specific to a particular API-level can go here. 
             */
            public static final int AppTheme=0x7f050001;
        }
    }
    

    APK文件

    APK文件是由框架自動(dòng)創(chuàng)建的。愛掏網(wǎng) - it200.com如果你想在移動(dòng)設(shè)備上運(yùn)行Android應(yīng)用程序,需要將其傳輸并安裝。愛掏網(wǎng) - it200.com

    資源

    它包含包括activity_main、strings、styles等資源文件。愛掏網(wǎng) - it200.com

    清單文件

    它包含有關(guān)包的信息,包括活動(dòng)、服務(wù)、內(nèi)容提供器等組件。愛掏網(wǎng) - it200.com

    有關(guān)清單文件更多信息,請(qǐng)?jiān)L問這里: AndroidManifest.xml文件 。愛掏網(wǎng) - it200.com

    聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
    發(fā)表評(píng)論
    更多 網(wǎng)友評(píng)論0 條評(píng)論)
    暫無評(píng)論

    返回頂部

    主站蜘蛛池模板: 无码人妻精品一区二区| 一本岛一区在线观看不卡| 国产免费播放一区二区| 88国产精品视频一区二区三区| 欧洲精品码一区二区三区| 久久er99热精品一区二区| 手机看片一区二区| 波多野结衣一区在线| 亚洲国产欧美国产综合一区| 无码精品黑人一区二区三区| 国产一区二区三区国产精品| 亚洲欧洲无码一区二区三区| 国产精品一区二区不卡| 亚洲午夜一区二区电影院| 在线精品亚洲一区二区小说| 一区二区视频免费观看| 国产成人一区二区三区高清| 精品久久久中文字幕一区| 深田咏美AV一区二区三区| 亚洲色无码专区一区| 国产精品一区二区久久| 无码播放一区二区三区| 亚洲一区二区三区久久| 国产精品分类视频分类一区| 国产一区二区三区91| 久久久无码精品国产一区| 麻豆一区二区三区精品视频| 日韩精品一区二区三区中文| 91在线精品亚洲一区二区| 日本在线视频一区二区| 亚洲bt加勒比一区二区| 久久无码人妻一区二区三区| 午夜福利av无码一区二区| 亚洲熟妇av一区| 国内精自品线一区91| 日本强伦姧人妻一区二区| 亚洲av区一区二区三| 相泽南亚洲一区二区在线播放| 视频一区在线播放| 手机看片一区二区| 久久毛片一区二区|