My Android notes (1) -- structure analysis of the hello world Program

Source: Internet
Author: User

Create an android project (Target 2.3.3 is used). eclipse automatically generates the following content --

-- This is a complete and runable "hello world" program.
The running result is:

Hello world and Demo_01Activity are displayed on the screen!

Then start to analyze the following program --
The following code snippet is available in AndroidManifest. xml:
Android: label = "@ string/app_name"
Android: name = ". Demo_01Activity">
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>

Demo_01Activity is the entry of the program.
Then content:
Package barry. android. demo;
Import android. app. Activity;
Import android. OS. Bundle;
Public class Demo_01Activity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

All activities must inherit the Activity class.
The Demo01_Activity class overwrites the onCreate method of the parent class. This method is executed when the program starts.
After executing the onCreate method of the parent class (row 7th), The setContentView (R. layout. main), this method is used to load content from the layout configuration file to the Activity.
SetContentView (R. layout. main) parameter -- R. layout. main -- R. java is an automatically generated resource file. The resources we use (images, strings, configuration files, etc. in the res folder) will be stored in the R. the corresponding ing is automatically generated in java. When referencing a resource, you only need to reference the corresponding ing in the R file. R. layout. main corresponds to main. xml in the res/layout/path.
Content of main. xml:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = ""
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>

<LinearLayout> labels indicate that the layout of the control is linear. android: orientation = "vertical" indicates that the layout is vertical. android: layout_width = "fill_parent", android: layout_height = "fill_parent" indicates that the display mode is full of parent controls (the parent control of the Activity is the entire screen ).
The <LinearLayout> label contains a <TextView> text box label, indicating that a text box is displayed, and its three attributes indicate: the height of the text box is full of the parent control, the width is automatically adapted to the content, and the display String "@ String/hello ".
@ String/hello refers to the string. xml in the res/values/directory. The value is "hello" and the value is "Hello World, Demo_01Activity !" --
String. xml content:
<? Xml version = "1.0" encoding = "UTF-8"?>
<String name = "hello"> Hello World, Demo_01Activity! </String>
<String name = "app_name"> Demo_01 </string>

Icon displayed in the mobile phone program menu --,
And the program title display, which is also configured by the code in AndroidManifest. xml:

Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
The icon provides three resolutions by default, which are determined by the system based on the resolution of the mobile phone.


Therefore, the preceding figure is displayed when demo_01 is run. -- A simple hello world Program.


From Wolf's second nest

Related Article

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: 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.