Android Study Notes (5): Activity and Main. xml files

Source: Internet
Author: User

In Andriod Study Notes (3): Andriod program framework, we have a preliminary understanding of the main. xml file. This time, we will first learn the relationship between Activity and main. xml.

Although we can use java code to compile the UI, the more common method is to use the XML-based Layout file, which is used to describe the relationship between the widget and the container. This makes it easy for us to read and design the UI independently, and also makes some IDE tools provide intuitive GUI.

1. modify main. xml

<! -- Linear layout. From top to bottom, the orientation is determined by orientation. fill_parent is used to fill the parent control. Full Screen is used here. -->
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<! -- If the code for this widget in Java source code requires an id,Andriod: Id = "@ + ID/<Name>"In R. java, the corresponding Code correspondsR. id. <name>In this example, we found in R. java: R. id. myTextView = 0x7f040000. We can use name to correspond to this space in the program. -->
<TextView
Android: id = "@ + id/myTextView"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<! -- Add a new control button, which is the widget name. If we define our own widget (which is andriod. view. view subclass), we need to provide the complete package path, such as com. wei. andriod. myWidget ). -->
<Button
Android: id = "@ + id/myButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</LinearLayout>

2. In the source code, obtain the instance by the name defined in xml.

Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
// R. layout. main is in the R. main in layout defined by the R class in Java, in the format of R. layout. <layout XML file name> is the corresponding Res/layout/main. XML file.
Setcontentview (R. layout. main);
Textview mytextview = (textview)FindViewById(R. Id. mytextview );
Mytextview. settext ("My activity ");
Button mybutton = (button)FindViewById(R. Id. mybutton );
Mybutton. settext ("My buttons ");
}

3. Run ,.

Related links:
My Android development articles

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.