Basic tutorial for Android Program Development (2)

Source: Internet
Author: User

UseXMLOrchestration

 

What you just completed"Hello, world"The example is called"ProgramInformatization". This means that you can directly build your application interface.Source code. If you have already completed many interface programs, you may be familiar with the weakness of such methods: a small change to the layout willCodeA headache. If you forgetViewThis can also cause code errors and interface problems that waste your debugging time.

 

That's whyAndroidProvides an alternative way to build an interface.:Based onXML. The simplest way to explain this concept is to present an example. We will use the project we just created for demonstration to achieve the same interface effect.

 

 
<? XML version = "1.0" encoding = "UTF-8"?>
 
<Textview xmlns: Android = "http://schemas.android.com/apk/res/android"
 
Android: layout_width = "fill_parent"
 
Android: layout_height = "fill_parent"
Android: text = "Hello, Android"/>

 

AndroidxmlThe general structure of the layout file is very simple. It is a label tree, and any label isViewClass Name. In this example,It is a simple tree with only one element.Textview. You can use anyViewThe class name is used as the label name. Include custom in your codeViewClass. This structure can easily build an interface, which is simpler than the structure and syntax you use in the source code. The design of this model is inspiredWebDevelopment. Is the mode that separates the interface from the application logic.

 

In this example, some of them areXMLThe following are their meanings.:

 

properties

meaning

xmlns: Android

This is the declaration of the XML namespace. It is a tool for Android, public attributes to be involved have been defined in the XML namespace. The outermost tag of each android layout file must have this attribute.

Android: layout_width

This attribute defines the available width of the view on the screen.

Android: layout_height

This attribute defines the available height of the view on the screen.

Android: Text

sets the text content contained in textview, which is currently set to "hello, android "Information

 

 

The above isXMLLayout file, but where do you need to put it? It will be placed in your project directoryRes/Folder."Res"Yes"Resources"It is a folder that stores all non-code resources, including images, localized strings, andXMLLayout file.

TheseEclipseThe plug-in has been created for you. In our example above, we didn't use it. In the package browser, expandResDirectoryLayout.And editMain. XML,Replace the text content and save it.

 

Now, in the package browsing status, open the folder namedR. JavaYou will see the following content:

 

 
Public final class r {
 
Public static final class ATTR {
 
};
 
Public static final class drawable {
Public static final int icon = 0x7f020000;
 
};
 
Public static final class layout {
 
Public static final int main = 0x7f030000;
 
};
 
Public static final class string {
Public static final int app_name = 0x7f040000;
 
};
 
};

 

 

A project'sR. JavaFile is an index file that defines all resources. Using this class is like using a stenographer to reference resources contained in your project. This is especially powerfulEclipseThis typeIDEBecause it enables you to quickly and interactively locate the specific references you are looking.

 

The important thing to note now is"Layout"Internal class and its member variables"Main ",The plug-in will notify you to add a newXMLLayout the file, and then generate thisR. JavaFile, such as adding new resources to your project, you will seeR. JavaAnd changed accordingly.

 

The last important thing is that you need to modify yourHelloandroidSource code to use the newXMLLayout your interface. Replace the simplified interface mode. The following shows how your new code looks. As you can see, the Code becomes simpler.

 

Public class helloandroid extends activity {

/** Called when the activity is first created .*/

@ Override

Public void oncreate (bundle icicle ){

Super. oncreate (icicle );

Setcontentview (R. layout. Main );

}

}

 

When you make these changes, do not just copy them, paste them into your code, and try to experience the code compilation features of R. java. You will find it very helpful to you.

 

After completing these changes, you can re-run your program, and then you will find that two different interface orchestration methods will produce the same effect.

 

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.