Android Four components activity (activities) and layout of the creation and loading layout

Source: Internet
Author: User

Android Four components activity (activities) and layout of the creation and loading layout

What is activity?

An activity is a component that contains a user interface that is used primarily to interact with users, and an application can contain 0 or more activities.

Detailed procedures for manually creating activity

 So far, you haven't created the activity manually, and in the first Android project, Helloworldactivity was created by ADT, and creating the activity manually can deepen our understanding, so it's time to do it yourself.

First of all, you need to create a new Android project, the project name can be called Activitytest, the package name we will use the default value of Com.example.activitytest. Because we created the activity manually, we didn't need to tick the create activity when we created the Android project, and it was ticked by default.

Click Finish, the project is created, and one thing to note is that we recommend that you close other Android projects when you are working on the current project.

Open only the items that are needed for your current job, or I promise you will suffer in this respect.

Create activity manually

Currently the SRC directory of the Activitytest project should be empty and you should add a package to the SRC directory first. Click File→new→package in the Eclipse navigation bar, and in the pop-up window, fill in the default package name that we used when we created the new project Com.example.activitytest, click Finish.

  

  

Now right click on the Com.example.activitytest package →new→class, will pop up the new Class dialog box, we create a new class named Firstactivity, and let it inherit from the Activity, click Finish to complete the creation, you need to know, Any activity in the project should rewrite the activity's OnCreate () method

  

When we get here, we've created the activity.

Let's say the creation and loading of layouts

The Android program is designed to be logical and view-separated, preferably one layout for each activity, and the layout to display the interface content, so let's create a layout file manually. Right-clicking the res/layout directory →new→android the XML layout file pops up the window where the layout files are created. We name the layout file First_layout, and the root element is selected by default as LinearLayout

  

  

Click Finish to complete the creation of the layout, as

  

This is the visual layout editor that ADT provides for us, and you can preview the current layout in the central area of the screen. There are two switch cards at the bottom of the window, graphical Layout on the left and first_layout.xml on the right. Graphical layout is the current visual layout editor, where you can not only preview the current layout, but also edit the layout by dragging and dropping. And First_layout.xml is the way to edit the layout through the XML file, now click on the first_layout.xml switch card, you can see the following code:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    </LinearLayout>

 

Since we have just chosen linearlayout as the root element when creating the layout file, there is now a linearlayout element in the layout file. Now let's make a little edits to this layout and add a button

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical" >6 7<Button8Android:id= "@+id/button1"9Android:layout_width= "Match_parent"Tenandroid:layout_height= "Wrap_content" Oneandroid:text= "button"/> A  -</LinearLayout>

A button element is added here, and several properties are added inside the button element. Android:id is to define a unique identifier for the current element, which can then be manipulated in code.

If you need to reference an ID in XML, use the @id/id_name syntax, and if you need to define an ID in XML, use the @+id/id_name syntax. The android:layout_width then specifies the width of the current element, which uses match_parent to make the current element as wide as the parent element. Android:layout_height Specifies the height of the current element, where wrap_content is used to indicate the height of the current element as long as it can contain exactly what is inside it. ANDROID:TEXT Specifies the text content displayed in the element

Now that the button has been added, you can point back to the graphical layout switch card and preview the current layout

Can be seen in the central preview area, the button has been successfully displayed, so that a simple layout is written to complete. So what we're going to do next is load this layout in the event.

Loading layouts

  

As you can see, the Setcontentview () method is called to load a layout for the current activity, and in the Setcontentview () method, we typically pass in the ID of a layout file.

Any resources added to the project will generate a corresponding resource ID in the R file, so the ID of the First_layout.xml layout we just created should now be added to the R file.

Registering in the Androidmanifest file

All activities must be registered in Androidmanifest.xml to take effect, so let's open androidmanifest.xml now to register firstactivity.

  

As you can see, the registration statement for the activity is placed in the tag, where the activity is registered through a tag. First of all we want to use Android:name to specify which activity to register, then what is the meaning of the. firstactivity that is filled in here? In fact, this is com.example.activitytest.FirstActivity abbreviation. Since the package name of the program has been specified in the outermost label as Com.example.activitytest, this part can be omitted and used directly when registering the activity. Firstactivity is enough. Then we use the Android:label to specify the contents of the title bar in the activity, the title bar is displayed at the top of the activity, you will see in a moment, the <activity> tag is included in the <intent-filter> tag, and added a

<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>

These two statements, said that if you want to firstactivity as our main activity of this program, that is, click on the desktop application icon when the first open is this activity, it must be added to these two statements. Also note that if your application does not declare any activity as the main activity, the program will still be installed normally, but you can not see in the launcher or open the program, the following we look at the results of the operation

At the top of the interface is a title bar that shows what we just specified when registering for the event. The title bar below is the interface written in the layout file First_layout.xml, you can see the button we just defined

Android Four components activity (activities) and layout of the creation and loading layout

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