Xây dựng giao diện cơ bản Nguyễn Nam Trung Email : nntrung.itc@gmail.com Nội dung Android Manifest Widget Layout ayout Android Manifest AndroidManifest.xml để mô tả những thông t
Trang 1Xây dựng giao diện cơ bản
Nguyễn Nam Trung
Email : nntrung.itc@gmail.com
Nội dung
Android Manifest
Widget
Layout ayout
Android Manifest
AndroidManifest.xml để mô tả những thông tin quan trọng của nó cho hệ thống Android
thống Android.
Tên cho Java package của ứng dụng.
Mô tả các thành phần của ứng dụng:
activity, service, broadcast receiver hoặc content provider.
Thông báo những permission.
Cho biết level thấp nhất của Android API
Cho biết level thấp nhất của Android API.
Android Manifest (tt)
<?xml version="1.0" encoding="utf
<
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas android com/apk/res/android"
Ví dụ nội dung tập tin AndroidManifest.xml:
<
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
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>
Trang 2Widget
Các widget sử dụng để tạo giao diện (Edit Text, Text View ) đều là lớp con của lớp View.
Một số widget hay được dùng để tạo giao diện:
TextView
EditText
Button
ToggleButton
CheckBox
RadioButton
RatingBar
ListView
TextView
• Được dùng để trình bày một dòng chữ (nhãn) trên layout.
• Một số thuộc tính:
android:typeface : thiết lập cách thức hiển thị nội dung
android:typeface : thiết lập cách thức hiển thị nội dung
android:textStyle : kiểu thể hiện font chữ (bold, italic, )
android:textColor : màu sắc font chữ
android:textSize : kích thước font chữ
• Ví dụ:
<TextView
android:id="@+id/txtView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/text_color"
android:textSize="28px"
android:text="You were expecting something profound?"
/>
EditText
• Là đối tượng nhằm cung cấp cho người sử dụng công cụ
để nhập vào các giá trị.
• Một số thuộc tính: Một số thuộc tính:
android:capitalize : tự động in hoa ký tự đầu
android:digits : cấu hình chỉ cho phép nhập số
android:singleLine : chỉ cho phép nhập trên một dòng
• Ví dụ:
<EditText
android:id="@+id/edtEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter the word here"
android:capitalize="words"
/>
Trang 3• Button là subclass của TextView , do đó hầu hết tất cả các thuộc tính được trình bày đều có thể áp dụng cho đối
tượng Button này tượng Button này.
<Button
android:id="@+id/cmdAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp”
android:background="@drawable/android_button"
/>
OnClickListener cmdAndroidClick = new OnClickListener() {
@Override public void onClick(View v) { // TODO Auto-generated method stub
Toast.makeText(formstuff.this, "Beep Bop",
Toast.LENGTH_SHORT).show();
} };
<ToggleButton
ToggleButton
• Button có thể thể hiện được 2 trạng thái đóng/mở.
gg
android:id="@+id/tcmdAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate On"
android:textOff="Vibrate Off"
/>
OnClickListener tcmdAndroidClick = new OnClickListener() {
@Override
@O e de public void onClick(View v) { // TODO Auto-generated method stub ToggleButton _tcmdAndroid = (ToggleButton)v;
if(_tcmdAndroid.isChecked() == true) //checked
else //unchecked }};
CheckBox
• Là đối tượng cung cấp cho người dùng công cụ lựa chọn hay không chọn một tiêu chí hoặc tùy chọn nào đó.
<Ch kB
OnClickListener chkAndroidClick
= new OnClickListener() {
<CheckBox
android:id="@+id/chkAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check it out"
/>
() {
@Override public void onClick(View v) { // TODO Auto-generated method stub
if (((CheckBox)v).isChecked() == true) //checked
else //uncheck }};
RadioButton
• Tương tự CheckBox nhưng tại một thời điểm chỉ có thể 1 lựa chọn duy nhất.
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/optRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
d id t t "R d"
android:text="Red"
android:checked="true”/>
<RadioButton
android:id="@+id/optBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue”/>
</RadioGroup>
Trang 4• Cung cấp cho người dùng công cụ đánh giá, xếp hạng.
<RatingBar
android:id="@+id/ratAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
/>
OnRatingBarChangeListener ratAndroidChange
= new OnRatingBarChangeListener() {
@Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
// TODO Auto-generated method stub Toast.makeText(formstuff.this, "Rating: " + rating, Toast.LENGTH_SHORT).show();
}};
Layout
Layout Linear Layout Relative Layout Table Layout Grid View
Linear Layout
• Là một ViewGroup cho phép hiển thị các thành phần con bên trong theo
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout height="fill parent"
một chiều nhất định (ngang hoặc dọc).
android:layout_weight="1"
>
</LinearLayout>
Trang 5Linear Layout – Properties
• android:orientation : sắp xếp các widget con của layout theo chiều ngang hay dọc.
• android:layout_weight : ngăn cách giữa các widget con của layout.
• android:gravity : thiết lập cách thức widget con được thể hiện như thế nào so với layout cha.
• android:padding : tạo khoảng cách giữa các widget con bên trong layout.
top
bottom
left
right
Relative Layout
• Sắp xếp các thành phần con dựa trên mối quan hệ với các thành phần khác
h ặ ới biê ủ l t hoặc với biên của layout.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent”>
<TextView
android:id="@+id/lblTypeHere"
android:text="Type here"
android:layout_width="fill_parent"
android la o t height " rap content”/>
<EditText
android:id="@+id/txtContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lblTypeHere”/>
</RelativeLayout>
Table Layout
• Sắp xếp các thành phần con dựa trên
1 lưới các ô ngang và dọc.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1”>
<TableRow>
<TextView
android:layout_column="1"
android:text="Open "
android:padding="3dip”/>p g p
<TextView
android:text="Ctrl-O"
android:layout_gravity="right"
android:padding="3dip”/>
</TableRow>
</TableLayout>
• Sắp xếp các thành phần con dựa theo tọa độ x, y của mỗi thành phần p
<AbsoluteLayout android:id="@+id/widget0“
android:layout_width="fill_parent“
android:layout_height="fill_parent">
<AnalogClock android:id="@+id/widget30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout x="10px" y _ p android:layout_y="2px"/>
<DatePicker android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="30px"
android:layout_y="202px"/>
</AbsoluteLayout>
Trang 6http://www.droiddraw.org
Q & A