[Transfer]android Learning summary----Activity view

Source: Internet
Author: User

What is activity?

    • Activity is a user interface program, in principle it provides the user with an interactive interface function.
    • It is the basic functional unit of an Android application.
    • The Activity itself has no interface.

What is View/viewgroups?

    • View/viewgroups: Represents the basic user interface unit on the Android platform.
    • View: Stores the layout and content for the specified screen rectangle area. Handle size and layout, draw, focus change, flip screen, keys etc...
    • Viewgroups: Include and manage sub-series of views and subordinate ViewGroup

Activity life cycle and information transfer

Life cycle of activity:

Activity status

    • When an Activity is on top of the screen (top of the stack), it is the active or running state
    • If an activity loses focus but still sees its picture (e.g. a new activity is not full-screen or translucent), the activity that loses focus is in the state of pause, which is still alive ("live" It means that its own state and data are present, and that it is kept in contact with the window management program. Only one case of this pause activity disappears, and when the system is out of memory, the system automatically determines that the unimportant activity is cleared.
    • If an activity is completely obscured by other activity, he still retains all the state and data, but because it is no longer visible to the user, his picture is hidden (the screen does not need to be updated), and when the system is out of memory, the activity of the Stop state Is the first to be considered to release memory.
    • When an activity is in the state of pause or stop, the system can either require the activity to end (finish) or simply remove (kill) it. When it needs to be presented again in front of the user, he must be able to fully reboot and restore the previous state.

Activity state transition Diagram

Activity Information Transfer :

(I think the message from Android is very much like the message in the WM.) )

When it comes to the transfer of Activity information, the key is the following two classes:

    • Intent

Android uses a special class Intent to jump from one screen to another, and Intent is used to describe what a program wants to do.

We all know that Android has four core components Activity, Service, broadcast receiver and content Provider, omit content Provider do not mention, then the remaining three components communication between the test what? This is intent!!. It can not only transmit information in the same application, but also can transmit information in different applications. (Information transmission methods and methods see: Intent and Intent Filter introduction)

Intent data structure is divided into two parts: one is action (such as VIEW EDIT PICK ...), one is the response to the data (the data is transmitted in the form of a URI).

    • Intent Filter

When Intent asks to do something, Intent filter is used to describe what the Activity can do (that is, Intent is the sender, and Intent filter is the receiver).

For example, an Activity should be able to display personal contact data, which means that you have to Intent Filter to explain how you want to handle personal contact data and present it with Action_view.

Intent can be divided into two categories, one is explicit intent, the other is implicit intent. The display of the intent is the name of the specified component, the implicit is not specified intent component name, requires Android according to the intent action, data, category, etc. to resolve the match. The target components (Activity, Service, broadcast Receiver) define their processing intent by setting their intent filter. If a component does not have a intent filter defined, it can only accept the intent of the processing display, and only the components that define intent filter can handle both implicit and displayed intent. (see: Intent and Intent Filter introduction)

When it comes to Intent filter, you have to say the Androidmanifest.xml file, because Intent filter is declared under this folder.

See: Androidmanifest.xml File parsing

Example program:

View/viewgroups Interface Layout:

The Android interface layout can be done in the resource file, and of course it is also available in the program (both of which we'll cover below). However, it is best to do it in a resource file, and then use Java to implement the logic, so that the interface display and logic can be independently managed.

layout parameters (Layoutparams)

Each ViewGroup class uses a nested class that inherits from Viewgroup.layoutparams, and Layoutparams contains a property type that defines the dimensions and position of the child node view.

A few common layout objects

Framelayout

    • The simplest layout object
    • You can then populate a separate object with a blank space deliberately reserved on the screen
    • All child elements are nailed to the upper left corner of the screen.
    • Cannot specify a location for child elements

LinearLayout

    • Aligns all child elements in one direction (vertical or horizontal). All child elements are stacked with one another.
    • A vertical list each row has only one child element (no matter how wide they are)
    • A horizontal list is just the height of a column (filled with the height of the highest element)
    • Main.xml code sample

<?xml version= "1.0" encoding= "Utf-8"?>

<!--Layout object is LinearLayout--
<linearlayout
<!--declaration namespace –>
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical" <!--vertical distribution –>
Android:layout_width= "Fill_parent" <!--the layout object is wider than the screen width –>
android:layout_height= "Fill_parent" <!--the layout object is high for screen height –> >

<!--child elements TextView --
<textview

Android:layout_width= "Fill_parent"<!--sub-element width –>
android:layout_height= "Wrap_content"<!--sub-element height –>
<!--the text type displayed is a string content object named Hello in the value folder –>
android:text= "@string/hello"
<!--child element ID value can be used;-> when called in a Java file
<!--"+" causes R.java to be automatically generated under the name Test_1 object of type ID –>
Android:id= "@+id/test_1"
Android:
Android:textcolor= "#00ff00"
Android:textstyle= "Bold"
/>
<textview
Android:id= "@+id/test_2"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
Android:textcolor= "#ff0000"
Android:
android:textstyle= "Italic" >
</TextView>
</LinearLayout>

:

Tablelayout

    • Like a table, the handle element is placed in [row] [column]
    • Do not display line numbers, column numbers, or cell boundary lines
    • Cells cannot span rows

Absolutelayout

    • Enables child elements to indicate the exact x/y coordinates displayed on the screen
    • (0,0) point is the upper-left corner of the screen
    • Allow elements to overlap (not recommended)
    • It is generally not recommended to use this layout object because in absolute

Relativelayout

    • Let child elements specify their position relative to other elements (specified by ID) or relative to the parent layout object

Resource file directory

Let's take a look at the structure and functionality of the Resource directory (project name Activitytest):

As shown in the following:

    • Bin directory: It contains the class file and the. apk installation file after the completion of the compilation.
    • Gen Directory: The lowest level of the directory is a R.java file (this file is important to invoke resources in the program)
    • Res Directory: Resource folder

Drawable: Placing a resource picture

Layout: An XML file that places the interface layout

Values: The string color ID used in the placement of the interface layout, etc...

    • SRC Directory: Java code directory

Specific use of resource files, see: Android Project Resource access

Layout settings in a Java program

Examples of programs:

Package Zyp. Activity;

To import a package to use

import android.app.Activity;

import Android.graphics.Color;

import Android.os.Bundle;

import android.widget.LinearLayout;

import Android.widget.TextView;

Import class Test extends Activity {

//Define the objects used

Private LinearLayout mylayout;

Private Linearlayout.layoutpatams LAYOUTP;

Private int WC = linearlayout.layoutpatams. wrap_content;

Private TextView BLACK_TV;

@Override

Public void onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

//Instantiate a LinearLayout layout object

mylayout= new linearlayout (this);

//Set the layout of LinearLayout to vertical layout

mylayout. SetOrientation (linearlayout. VERTICAL);

//Load to home screen

Setcontentview (mylayout);

//Instantiate a LinearLayout layout parameter to add the view

LAYOUTP = new linearlayout.layoutparams (WC ,WC );

//Instantiate a TextView object

Black_tv = new TextView (this);

//Add TextView to the LinearLayout layout

mylayout. AddView (black_tv, layoutp);

//Set text color and text content

black_tv. SetText ("Black");

black_tv. SetTextColor (Color. BLACK);

}

}

[Transfer]android Learning summary----Activity view

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.