Cấu trúc chi tiết của một project Android
Trong bài này chúng ta xem cấu trúc chi tiết của một project Android thông qua ví dụ.
Mốt ứng dụng Android chứa các thành phần (component) khác nhau như mã nguồn java, string resources, images, manifest file, apk file vv . Hãy xem 1 cấu trúc của project Android:
Java Source Code
Hãy xem file Java được tạo bởi Eclipse IDE
File: MainActivity.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 là một class, mà lớp lại tạo ra một window mặc định trên màn hình. Ở đây chúng ta có thể đặt thêm các thành phần (components) khác: Button, EditText, TextView, Spinner v.v. Nó giống như Frame của Java AWT.
Activity có các phương thức tạo ra vòng đời : onCreate, onStop, OnResume v.v.
(2) Phương thức onCreate method được gọi khi Activity lần đầu được tạo ra.
(3) Phương thức setContentView(R.layout.activity_main) dùng để kết nối Layout vào Activity. Trong trường hợp này layout của chúng ta là activity_main.xml.
File: activity_main.xml
<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> Ta thấy có một TextView được tạo tự động, nhưng chuỗi thông báo này được định nghĩa trong file strings.xml. @string/hello_world cung cấp chuỗi thông báo cho TextView. Giá trị của thuộc tính hello_world được định nghĩa trong tập tin strings.xml.
File: strings.xml
<?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> Chúng ta có thể thay đổi thuộc tính hello_world từ tập tin này
Tập tin R.java
Tập tin R.java được tạo ra một cách tự động, nó chứa cá ID của tất cả các tài nguyên trong thư mục res. Bất cứ khi nào bạn tạo một componet trong activity_main, thì một ID được tạo ra trong tập tin R.java. ID này sẽ được sử dụng trong source Java sau này.
File: R.java
/* 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; } } Tập tin APK
Tập tin apk được tạo tự động. Nếu bạn muốn ứng dụng của mình chạy trên các thiết di động.
Thư mục Resources
Nó chứa các tập tin tài nguyên : activity_main, strings, styles v.v.
Tập tin AndroidManifest.xml
Nó chứa các thông tin về package bao gồm các thành phần (component): activity_main, strings, styles v.v
Xem thêm thông tin về tập tin AndroidManifest.xml
Cập nhật công nghệ từ Youtube tại link: https://www.youtube.com/channel/UCOxeYcvZPGf-mGLYSl_1LuA/videos
Để tham gia khóa học công nghệ truy cập link: http://thuvien.hocviendaotao.com
Mọi hỗ trợ về công nghệ email: dinhanhtuan68@gmail.com