Lập trình Android tiếng Việt - Chapter 5: Android Basic XML Layouts

 

Summary of Commonly-used Android containers

 

LinearLayout (the box model),

 

RelativeLayout (a rule-based model), and

 

TableLayout (the grid model), along with

 

ScrollView, a container designed to assist with implementing scrolling containers.

 

Other (ListView, GridView, WebView, MapView, ) discussed later

 

pptx38 trang | Chuyên mục: Android | Chia sẻ: dkS00TYs | Lượt xem: 2582 | Lượt tải: 1download
Tóm tắt nội dung Lập trình Android tiếng Việt - Chapter 5: Android Basic XML Layouts, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
ch thước 	android:layout_width and android:layout_height để cung cấp thông tin giải quyết vấn đề empty space. Các giá trị cho height và width: Xác định một kích thước cụ thể, chẳng hạn 125dip (device independent pixels) wrap_content, nghĩa là widget sẽ lấy đủ không gian nó cần (natural space), nếu nó quá lớn thì Android có thể dùng kiểu word-wrap để co nó lại cho vừa. fill_parent, nghĩa là widget sẽ lấy hết không gian còn lại của container nếu còn thừa. 12 5. Android – UI – Basic XML Layouts Linear Layout 12 1.2 	Linear Layout: Fill Model Row-wise Use all the row Specific size: 125dip 125 dip entire row (320 dip on G1) G1 phone resolution is: 320 x 480 dip (3.2 in). 13 5. Android – UI – Basic XML Layouts Linear Layout 13 1.2 	Linear Layout: Weight Cấp phát không gian theo tỷ lệ cho các widget trong một view. Gán một giá trị (1, 2, 3,…) cho android:layout_weight để quy định tỷ lệ không gian dành cho widget đó. Example Both the TextView and the Button widgets have been set as in the previous example. Both have the additional property android:layout_weight="1" whereas the EditText control has android:layout_weight="2" Default value is 0 Takes: 2 /(1+1+2) of the screen space 14 5. Android – UI – Basic XML Layouts Linear Layout 14 1.3 	Linear Layout: Gravity It is used to indicate how a control will align on the screen. By default, widgets are left- and top-aligned. You may use the XML property 	android:layout_gravity=“…” 	to set other possible arrangements: 	left, center, right, top, bottom, etc. Button has right gravity 15 5. Android – UI – Basic XML Layouts Linear Layout 15 1.3 	CAUTION: gravity vs. layout_gravity Phân biệt giữa: 	android:gravity  	quy định nơi đặt nội dung của một đối tượng bên trong chính đối tượng đó theo trục x và trục y.  	android:layout_gravity  	vị trí của view đối với container của nó. android:gravity="center" android:layout_gravity="center" 16 5. Android – UI – Basic XML Layouts Linear Layout 16 1.4 	Linear Layout: Padding Quy định không gian giữa các biên của “ô” chứa widget và nội dung của chính widget đó. If you want to increase the internal whitespace between the edges of the and its contents, you will want to use the: android:padding property or by calling setPadding() at runtime on the widget's Java object. Note: Padding is analogous to the margins on a word processing document. 17 5. Android – UI – Basic XML Layouts Linear Layout 17 1.3 	Linear Layout: Padding and Margin 18 5. Android – UI – Basic XML Layouts Linear Layout 18 1.3 	Linear Layout: Internal Margins Using Padding Example: The EditText box has been changed to display 30dip of padding all around ... 19 19 5. Android – UI – Basic XML Layouts Linear Layout 19 1.4 	Linear Layout: (External) Marging By default, widgets are tightly packed next to each other. To increase space between them use the android:layout_margin attribute ... Increased inter-widget space 20 5. Android – UI – Basic XML Layouts Relative Layout 20 2. Relative Layout RelativeLayout sắp xếp các widget theo quan hệ giữa các widget trong cùng một container và với container. A C B Example: A is by the parent’s top C is below A, to its right B is below A, to the left of C 21 5. Android – UI – Basic XML Layouts Relative Layout 21 2. Relative Layout - Referring to the container Some positioning XML (boolean) properties mapping a widget according to its location respect to the parent’s place are: android:layout_alignParentTop says the widget's top should align with the top of the container android:layout_alignParentBottom the widget's bottom should align with the bottom of the container android:layout_alignParentLeft the widget's left side should align with the left side of the container android:layout_alignParentRight the widget's right side should align with the right side of the container android:layout_centerInParent the widget should be positioned both horizontally and vertically at the center of the container android:layout_centerHorizontal the widget should be positioned horizontally at the center of the container android:layout_centerVertical the widget should be positioned vertically at the center of the container 22 5. Android – UI – Basic XML Layouts Relative Layout 22 2. Relative Layout – Referring to other widgets The following properties manage positioning of a widget respect to other widgets: android:layout_above indicates that the widget should be placed above the widget referenced in the property android:layout_below indicates that the widget should be placed below the widget referenced in the property android:layout_toLeftOf indicates that the widget should be placed to the left of the widget referenced in the property android:layout_toRightOf indicates that the widget should be placed to the right of the widget referenced in the property 23 5. Android – UI – Basic XML Layouts Relative Layout 23 2. Relative Layout – Referring to other widgets – cont. android:layout_alignTop indicates that the widget's top should be aligned with the top of the widget referenced in the property android:layout_alignBottom indicates that the widget's bottom should be aligned with the bottom of the widget referenced in the property android:layout_alignLeft indicates that the widget's left should be aligned with the left of the widget referenced in the property android:layout_alignRight indicates that the widget's right should be aligned with the right of the widget referenced in the property android:layout_alignBaseline indicates that the baselines of the two widgets should be aligned 24 5. Android – UI – Basic XML Layouts Relative Layout 24 2. Relative Layout – Referring to other widgets In order to use Relative Notation in Properties you need to consistently: Put identifiers (android:id attributes) on all elements that you will need to address. Syntax is: @+id/... (for instance an EditText box could be XML called: android:id="@+id/ediUserName") Reference other widgets using the same identifier value (@+id/...) already given to a widget. For instance a control below the EditText box could say: android:layout_below="@+id/ediUserName" 25 5. Android – UI – Basic XML Layouts Relative Layout 2. Relative Layout – Example 25 26 5. Android – UI – Basic XML Layouts Relative Layout 2. Relative Layout – Comment (as of Aug. 2009) Use the Eclipse ADT Layout Editor for laying out RelativeLayouts. DroidDraw is of very little help in this respect. 26 27 5. Android – UI – Basic XML Layouts Table Layout 3. Table Layout Android's TableLayout allows you to position your widgets in a grid made of identifiable rows and columns. Columns might shrink or stretch to accommodate their contents. TableLayout works in conjunction with TableRow. TableLayout controls the overall behavior of the container, with the widgets themselves positioned into one or more TableRow containers, one per row in the grid. 27 28 5. Android – UI – Basic XML Layouts Table Layout 3. Table Layout Rows are declared by you by putting widgets as children of a TableRow inside the overall TableLayout. The number of columns is determined by Android ( you control the number of columns in an indirect way). So if you have three rows, one with two widgets, one with three widgets, and one with four widgets, there will be at least four columns. 28 0 1 0 1 2 0 1 2 3 29 5. Android – UI – Basic XML Layouts Table Layout 3. Table Layout However, a single widget can take up more than one column by including the android:layout_span property, indicating the number of columns the widget spans (this is similar to the colspan attribute one finds in table cells in HTML) 29 30 5. Android – UI – Basic XML Layouts Table Layout 3. Table Layout Ordinarily, widgets are put into the first available column of each row. In the example below, the label (“URL”) would go in the first column (column 0, as columns are counted starting from 0), and the TextField would go into a spanned set of three columns (columns 1 through 3). 30 Label (URL) EditText EditText-span EditText-span Column 0 Column 1 Column 2 Button Cancel Column 3 Button OK android:layout_span="3" android:layout_columns="2" 31 5. Android – UI – Basic XML Layouts Table Layout 3. Table Layout – Example 31 Skip columns: 0, 1 Strech up to column 3 Note to the reader: Experiment changing layout_span to 1, 2, 3 32 5. Android – UI – Basic XML Layouts Table Layout 3. Table Layout By default, each column will be sized according to the "natural" size of the widest widget in that column. If your content is narrower than the available space, you can use the TableLayout property: 	 	android:stretchColumns =“…” Its value should be a single column number (0-based) or a comma-delimited list of column numbers. Those columns will be stretched to take up any available space yet on the row. 32 33 5. Android – UI – Basic XML Layouts Table Layout 3. Table Layout 	In our running example we stretch columns 2, 3, and 4 to fill the rest of the row. 33 ... ... TODO: try to stretch one column at the time 1, then 2, and so on. 34 5. Android – UI – Basic XML Layouts ScrollView Layout 4. ScrollView Layout Dùng ScrollView khi dữ liệu cần hiển thị dài hơn 1 trang màn hình. Cho phép kéo thanh cuốn để xem từng phần. Tương tự 1 trang web. 34 35 5. Android – UI – Basic XML Layouts ScrollView Layout 4. Example ScrollView Layout 35 36 5. Android – UI – Basic XML Layouts ScrollView Layout 4. Example ScrollView Layout 36 Scroller Combining an ImageView & TextView in a horizontal Linear Layout Simple TextView 37 5. Android – UI – Basic XML Layouts Absolute Layouts 5. Miscellaneous. Absolute Layout (đã lạc hậu, nên thay bằng layout khác) layout cho phép quy định chính xác tọa độ (x,y) của các thành phần trong container. Absolute layouts are less flexible and harder to maintain than other types of layouts without absolute positioning. 37 38 5. Android – UI – Basic XML Layouts Absolute Layouts 5. Miscellaneous Absolute Layout (cont.) 38 Button location 39 5. Android – UI – Basic XML Layouts Basic XML Layouts - Containers Questions? 39 

File đính kèm:

  • pptxLập trình Android tiếng Việt - Chapter 5 Android Basic XML Layouts.pptx