Android - Giao diện người dùng sử dụng XML Layouts
lượt xem 58
download
Các lớp học Xem đại diện cho khối xây dựng cơ bản cho các thành phần giao diện người dùng. Xem A chiếm một diện tích hình chữ nhật trên màn hình và chịu trách nhiệm cho các bản vẽ và xử lý sự kiện. Xem là lớp cơ sở cho các vật dụng, được sử dụng để tạo ra các thành phần giao diện tương tác (các nút, các trường văn bản, vv.) Các lớp con ViewGroup là lớp cơ sở để bố trí, được đựng vô hình mà giữ Lượt xem khác (hoặc các ViewGroups) và xác định...
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Android - Giao diện người dùng sử dụng XML Layouts
- Android – User Interfaces Using XML Layouts Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © 2008-2009 CommonsWare, LLC. ISBN: 978-0-9816780-0-9 & Android Developers http://developer.android.com/index.html
- Android – UI - User Interfaces The View Class • The View class represents the basic building block for user interface components. • A View occupies a rectangular area on the screen and is responsible for drawing and event handling. • View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). • The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties. 2
- Android – UI - User Interfaces Using Views All of the views in a window are arranged in a single tree. You can add views either from code or by specifying a tree of views in one or more XML layout files. Once you have created a tree of views, there are typically a few types of common operations you may wish to perform: 1. Set properties: for example setting the text of a TextView. Properties that are known at build time can be set in the XML layout files. 2. Set focus: The framework will handled moving focus in response to user input. To force focus to a specific view, call requestFocus(). 3. Set up listeners: Views allow clients to set listeners that will be notified when something interesting happens to the view. For example, a Button exposes a listener to notify clients when the button is clicked. 4. Set visibility: You can hide or show views using setVisibility(int). 3
- Android – UI - User Interfaces A brief sample of UI components Layouts Linear Layout Relative Layout Table Layout A LinearLayout is a A RelativeLayout is a ViewGroup A TableLayout is a GroupView that will lay that allows you to layout child ViewGroup that will lay child View elements elements in positions relative to child View elements into vertically or horizontally. the parent or siblings elements. rows and columns. 4
- Android – UI - User Interfaces A brief sample of UI components Widgets GalleryView TabWidget Spinner DatePicker Form Controls A DatePicke is a widget Includes a variety of typical that allows the user to form widgets, like: select a month, day and image buttons, year. text fields, checkboxes and radio buttons. 5
- Android – UI - User Interfaces A brief sample of UI components WebView MapView AutoCompleteTextView ListView It is a version of the EditText A ListView is a View that widget that will provide shows items in a vertically auto-complete suggestions scrolling list. The items are as the user types. The acquired from a ListAdapter. suggestions are extracted from a collection of strings. 6
- Android – UI - User Interfaces What is an XML Layout? An XML-based layout is a specification of the various UI components (widgets) and the relationships to each other – and to their containers – all written in XML format. Android considers XML- based layouts to be resources, and as such layout files are stored in the res/layout directory inside your Android project. 7
- Android – UI - User Interfaces What is an XML Layout? ASIDE You could create Layout XML files using UI tools such as: • Eclipse ADT UI Designer (getting better but still…) DroidDraw (http://www.droiddraw.org/) • Asset Studio (http://android-ui-utils.googlecode.com/ • hg/asset-studio/dist/index.html) 8
- Android – UI - User Interfaces What is an XML Layout? Each XML file contains a tree of elements specifying a layout of widgets and containers that make up one View (shown later). The attributes of the XML elements are properties, describing how a widget should look or how a container should behave. Example: If a Button element has an attribute value of android:textStyle = "bold" that means that the text appearing on the face of the button should be rendered in a boldface font style. 9
- Android – UI - User Interfaces An example The application places a button to occupy the screen. When clicked the button’s text shows current time. import java.util.Date; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class AndDemo extends Activity { Button btn; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); btn = (Button) findViewById(R.id.myButton); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { updateTime(); } }); }// onCreate // private void updateTime() { btn.setText(new Date().toString()); } } 10 10
- Android – UI - User Interfaces An example This is the XML-Layout definition The root element needs to declare the Android XML namespace: xmlns:android="http://schemas.android.com/apk/res/android" All other elements will be children of the root and will inherit that namespace declaration. Because we want to reference this button from our Java code, we need to give it an identifier via the android:id attribute. 11 11
- Android – UI - User Interfaces An example cont. The remaining attributes are properties of this Button instance: • android:text indicates the initial text to be displayed on the button face (in this case, an empty string) • android:layout_width and android:layout_height tell Android to have the button's width and height fill the "parent“ container, in this case the entire screen. 12 12
- Android – UI - User Interfaces UI Hierarchy Look for your SDK folder, usually: C:/your_sdk_path/android_sdk_windows/tools UI Tree The utility HierarchyViewer displays the UI structure of the current screen shown on the emulator or device. ( Execute app on emulator, execute HierarchyViewer, click on Emulator > Refresh Screenshot ) 13 13
- Android – UI - User Interfaces Android Layouts Each element in the XML Layout is either a View or ViewGroup object 14 14
- Android – UI - User Interfaces Android Layouts Displaying the Application’s View The Android UI Framework paints the screen by walking the View tree by asking each component to draw itself in a pre-order traversal way. Each component draws itself and then asks each of its children to do the same. See: Android – Application Development, by R. Rogers et al. O’Reilly Pub. 2009, ISBN 978-0-596-52147-0 15 15
- Android – UI - User Interfaces Android Layouts Example: Display UI Hierarchy Using SDK older than r8. vertical Horizontal 1 Horizontal 2 See: Android – Application Development, by R. Rogers et al. O’Reilly Pub. 2009, ISBN 978-0-596-52147-0 16 16
- Android – UI - User Interfaces Android Layouts Example: Display UI Hierarchy (Using SDK Revision 8) vertical UI Tree Horizontal 1 Horizontal 2 17 17
- Android – UI - User Interfaces Android Layouts Example: Display UI Hierarchy See: Android – Application Development, by R. Rogers et al. O’Reilly Pub. 2009, ISBN 978-0-596-52147-0 18 18
- Android – UI - User Interfaces Common Layouts There are five basic types of Layouts: Frame, Linear, Relative, Table, and Absolute. 1. FrameLayout FrameLayout is the simplest type of layout object. It's basically a blank space on your screen that you can later fill with a single object — for example, a picture that you'll swap in and out. All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot specify a different location for a child view. Subsequent child views will simply be drawn over previous ones, partially or totally obscuring them (unless the newer object is transparent). 19 19
- Android – UI - User Interfaces Common Layouts 2. LinearLayout LinearLayout aligns all children in a single direction — vertically or horizontally depending on the android:orientation attribute. All children are stacked one after the other, so a • vertical list will only have one child per row, no matter how wide they are, and a • horizontal list will only be one row high (the height of the tallest child, plus padding). A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child. 20 20
CÓ THỂ BẠN MUỐN DOWNLOAD
-
5 điều iOS cần để kéo người dùng ra khỏi sự cám dỗ của Android
4 p | 157 | 24
-
40 thủ thuật Android cho người mới dùng Android
9 p | 97 | 21
-
Thiết lập video clip làm ảnh nền cho điện thoại Android
2 p | 154 | 18
-
Phương pháp đề phòng virus khi sử dụng điện thoại Android
14 p | 115 | 11
-
Trao đổi dữ liệu giữa điện thoại Android qua Wifi
4 p | 154 | 10
-
.Cẩm nang Android dành cho người mới sử dụng
11 p | 77 | 9
-
Mẹo sử dụng máy tính để bàn như smartphone
9 p | 97 | 9
-
Di động cho mọi người: Các hoạt động (Activities) và biểu tượng (icons) trong vòng đời ứng dụng Android
30 p | 77 | 8
-
.Các bước bảo mật thiết bị Android
7 p | 85 | 8
-
Chia sẻ và truy cập dữ liệu trên điện thoại Android từ máy tính thông qua Wi-Fi.
7 p | 120 | 7
-
Tạo, chỉnh sửa, upload và chia sẻ tài liệu Online từ điện thoại Android
8 p | 87 | 7
-
5 mẹo tăng tốc điện thoại Android
5 p | 87 | 6
-
5 tính năng mới “đỉnh” nhất của Android 4.2
2 p | 60 | 6
-
Đồng bộ miễn phí các thiết bị Android
5 p | 76 | 6
-
9 mẹo giúp người dùng Android có thể tăng thời lượng pin sử dụng
14 p | 80 | 5
-
.Số người dùng iOS gấp đôi Android ở châu Âu
8 p | 56 | 5
-
Xóa lược sử duyệt Web trên thiết bị Android
15 p | 107 | 3
-
SkyAMP app: Truy cập thư viện nhạc không dây trên điện thoại Android
8 p | 79 | 3
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn