Android Learning Notes (iv) View and layout

Source: Internet
Author: User

Http://www.cnblogs.com/cxcco/archive/2011/12/09/2282701.html

Second, the text

The so-called people rely on clothing horse saddle, a want to attract users of the application, light function is not much role, beautiful rich UI is also extremely important. How can you create a beautiful, actionable application?

1. View Introduction

In Android, all visual components are inherited from the view class, and users build the user interface through view and ViewGroup or by extending their classes. A View object handles its own measurement, layout, drawing, focus change, scrolling, key/gesture interaction with the rectangular area on the screen. As an object in the user interface, view is also an interactive event receiver that interacts with the user.

(from official website)

In Android, classes are inherited as shown above, and control is used to implement relatively simple functions, while widgets are used to combine controls and build more complex controls, such as user-defined components. Control can also be converted to widgets with each other.

2. Layout Introduction

In an Android application, a view has a lot of controls, how can you control the position and arrangement of those controls? In the Web page we can layout by Table,div in HTML, and in Android we use layout to implement layouts.

There are several main types of layout in Android:

    • LinearLayout

LinearLayout linear layout, the controls contained in the LinearLayout are arranged in a row or column in order.

The main introduction of his two main attributes:

      • orientation--direction, when set to horizon, the controls in layout are lined up, and when set to vertical, the controls in layout are lined up in one row.
      • fillmode--fill mode, that is, set android:layout_width and Android:layout_height, can be three kinds of values: specific pixel values, wrap_content, the actual length of the text in the control display, Fill_ Parent, which indicates that all remaining free space is populated. Specific examples can be viewed on the official website, here is not an example.
    • Relativelayout

Relativelayout relative layout, it relies on the relative position of the parent container or other controls in a container to locate.

A total of four main properties in two cases:

      • Relative parent container:

Android:layout_alignparenttop: The top of the control is aligned with the top of the parent container.

Android:layout_alignparentbottom: The bottom of the control is aligned with the bottom of the parent container.

Android:layout_alignparentleft: Aligns the left edge of the control with the parent container.

Android:layout_alignparentright: The right side of the control is aligned with the left edge of the parent container.

      • Relative to other controls in the same container:

Android:layout_above: Indicates that this control is on top of another control.

Android:layout_below: Indicates that the control is under another control.

Android:layout_toleftof: Indicates that this control is on the left side of another control.

Android:layout_torightof: Indicates that this control is on the right side of another control.

Android:layout_aligntop: Indicates that this control is aligned to the top of another control.

Android:layout_alignbottom: Indicates that this control is aligned with the bottom of another control.

Android:layout_alignleft: Indicates that this control is aligned to the left of another control.

Android:layout_alignright: Indicates that this control is aligned to the right of another control.

For examples, see the official website.

    • Tablelayout

Table layout, similar to the HTML table. A row is defined by TableRow, and if a control occupies more than one column you can set Android:layout_span, similar to the colspan of a table in HTML. By default, a control is placed in each column sequentially (column 0, column 1 ...), or in which column you can specify by Android:layout_column. If a column is too long or too short, you can increase or decrease the width of the column by Android:stretchcolumns and Android:shrinkcolumns.

For examples, see the official website.

    • Absolutelayout

The absolute layout, that is, Android does not provide any layout control, but by ourselves through the x-coordinate, the y-coordinate to control the position of the component, in the container inside the component location, the size of the needs of our own control.

Layout_x: Sets the x-coordinate of the subassembly.

Layout_y: Sets the y-coordinate of the subassembly

It is not recommended to use Absolutelayout for control layout, because the screen size of the user's phone is very small, if you use this to layout, then you think about your own workload.

3. Widget Introduction

Widgets are divided into system-provided and user-defined sections, which are described in the following notes for the User customization section. The Android system provides us with a standard view toolbox, and we just need to invoke these tools to create an application that looks consistent.

Here are some of the controls that the Android system provides for us:

? TextView

The standard read-only text label. It supports multi-line display, string formatting, and text wrap.

? EditText

A text input box that can be edited. It supports multiple lines of input and text wrapping.

? Listview

A viewgroup that creates and manages a set of display items in a list. The standard ListView uses TextView to display values within each string array.

? Spinner

The combo control, which displays a TextView and an associated ListView, is used to select an item from a list and display the selection in TextView. It also has a button that displays a selection box when pressed.

? Button

The standard button.

? CheckBox

A button of two states, representing checked or unchecked.

? RadioButton

Radio button.

More can view official help documentation.

4. Create a Menu

When you open an application, click on the phone's menu key, this time the menu option is pop out.

If you want to create a menu, you need to override the Onoptionsitemselected (Response menu selection), Oncreateoptionsmenu (Create menu menu) These two methods.

1 package Lib.demo;
2
3 Import android.app.Activity;
4 Import Android.os.Bundle;
5 Import Android.view.Menu;
6 Import Android.view.MenuItem;
7 Import Android.widget.TextView;
8
9 public class Android_demoactivity extends Activity {
Ten/** called when the activity is first created. */
One by one TextView tv=null;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
tv= (TextView) Findviewbyid (R.ID.TEXTVIEW1);
17}
18
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
/TODO auto-generated Method stub
22//The first parameter is which group the subkey belongs to
23//The second parameter is the unique ID of the subkey
24//The third parameter is the ordinal of the subkey that is displayed in the menu
25//Fourth parameter is the display name of the subkey
Menu.add (0, 1, 1, "Exit");
Menu.add (0, 2, 2, "about");
Super.oncreateoptionsmenu return (menu);
29}
30
@Override
public boolean onoptionsitemselected (MenuItem item) {
/TODO auto-generated Method stub
if (Item.getitemid () ==1)
35 {
();
37}
if (Item.getitemid () ==2)
39 {
Tv.settext (Item.gettitle ());
41}
super.onoptionsitemselected return (item);
43}
44
45}
Iii. Summary

I studied view and layout today, and at the end of the day I gave you an example of creating a menu. Layout control, for our future development and its importance, in the future development will be often exposed. Several common controls are described in detail in note five.

Android Learning Notes (iv) View and layout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.