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