You are on page 1of 5

Trong bi 1 mnh gii thiu s lc v cc thnh phn c bn ca Android cng

nh vic s dng XML lp trnh ng dng Android. Trong bi ny mnh s gii


thiu thm v Android Manifest v i su hn v vn lm vic vi View.
Android Manifest
Trong khung Package Explorer, pha di th mc res, bn s thy 1 file c tn
l AndroidManifest.xml. Mi ng dng u cn c AndroidManifest.xml m t
nhng thng tin quan trng ca n cho h thng Android bit. Let's look closer:
M:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/andro
id"
package="at.exam"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".Example"
android:label="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>

C th nhng cng vic m AndroidManifest.xml thc hin:


- t tn cho Java package ca ng dng.
- M t cc thnh phn (component) ca ng dng: activity, service, broadcast
receiver hoc content provider.
- Thng bo nhng permission m ng dng cn c truy nhp cc protected API
v tng tc vi cc ng dng khc.
- Thng bo nhng permission m cc ng dng khc cn c tng tc vi ng
dng hin thi.
- Thng bo level thp nht ca Android API m ng dng cn chy. (Android
1.0 l level 1, 1.1 l level 2, 1.5 level 3, 1.6 level 4 v 2.0 l level 5).
...
Hy xem th file AndroidManifest.xml ca chng trnh TooDo mnh ang xy
dng:
M:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/andro
id"
package="android.at"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".TooDo"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscre
en"
android:label="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />

</intent-filter>
</activity>
<activity android:name=".WorkEnter">
</activity>
<receiver android:name=".AlarmReceiver">
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission
android:name="android.permission.VIBRATE"/>
</manifest>
Main Activity ca chng trnh Too Do ny l activity TooDo. Ngoi ra mnh cn
c 1 Activity khc c tn l WorkEnter cho php nhp vo thi gian v ni
dung cng vic. 1 Broadcast Receiver c tn l AlarmReceiver nhn alarm gi
ti trong intent.
Khi alarm c nhn s c m thanh v rung (vibration). Tt c cng vic s c
vit trong code, nhng bt buc bn phi khai bo cc thnh phn c trong ng
dng vo AndroidManifest nu mun chng trnh hot ng.
Tng t, set permission truy nhp camera, internet, c contact... cng u
phi khai bo trong AM. T kha screenOrientation cho php thit lp giao din
khi vo ng dng theo chiu dc (portrait - mc nh) hay ngang (landscape),
theme cho php s dng style c sn ca android l full-screen (ko c thanh status
bar na).
Intent filter l b lc dng gii hn cc intent c s dng trong activity hay
receiver...
M:
<intent-filter>
<action
android:name="android.intent.action.VIEW"/>
<category
android:name="android.intent.category.DEFAULT"/>
<category
android:name="android.intent.category.BROWSABLE"/>

<data android:scheme="http"
android:host="www.google.com"
android:path="/m/products/scan"/>
</intent-filter>
B lc trn ch cho php intent m internet vi ng dn nh ngha sn
(http://www.google.com/m/products/scan)
Ok, hi vng mi ngi nm c chc nng c bn cng nh cch s dng
Android Manifest

Working with View


Trong bi 1 mnh gii thiu qua cch s dng Edit Text v Text View. Thc
cht cc View cn li cng c cch s dng tng t, bn s kt hp nhiu View
khc nhau cho ra giao din mnh mong mun. y mnh s cp nhiu ti
List View (theo kin mnh l View kh s dng nht).
Yu cu: Xy dng mt chng trnh cho php nhp ni dung cng vic v thi
gian ri list ra
B1: Vn bt u bng cch khi to mt Project mi: File -> New -> Android
Project.
Project name: Example 2
Build Target: Chn Android 1.5
Application name: Example 2
Package name: at.exam
Create Activity: Example
=> Kch nt Finish.
Giao din ta thit k y c 1 Linear Layout lm thnh phn chnh, cc thnh
phn con ca n gm 1 Edit Text (dng nhp ni dung cng vic), 1 Linear
Layout (li gm cc thnh phn con nhp gi v pht thc hin cng vic), 1
Button ( thm ni dung cng vic vo List View) v 1 List View dng list cc
cng vic bn nhp.
T kha lines c dng c nh s dng v nn s dng vi Edit Text thay v
dng mi wrap_content v nu sd wrap_content th Edit Text s t gin ra nu
dng nhp vo vt gii hn ng bao (lm hng giao din bn thit k).
T kha gravity thng bo cc thnh phn con s c sp xp ntn thnh phn

cha. y mnh dng "center" ngha l thnh phn con nm trung tm. Hy th
thm vo 1 Edit Text:
M:
android:gravity="center"

You might also like