From the original: http://blog.csdn.net/llping2011/article/details/9992941
One, Android layout
Any visual control in Android is inherited from Android.veiw.View, and the system provides two ways to set up the view:
- Use an XML file to configure the related properties of the view, and then create the corresponding view view based on the configuration file when the program starts.
- Use the corresponding class directly in your code to create the view.
To define a view using an XML file:
Each Android project has a res/layout directory under the source directory, which is used to store layout files. Layout files are typically named after the activity's name, with. xml as the suffix. When creating a component in XML, you need to specify an ID for the component, such as: The android:id= "@+id/name" system automatically creates the corresponding R resource class variable in the Gen directory.
To use a view in your code:
When you create each activity in your code, you typically call Setcontentview () in the OnCreate () method to load the specified XML layout file, and then you can get the control of the corresponding ID created in the layout file by Findviewbyid (). such as button.
1 Public classMainactivityextendsappcompatactivity2 {3 PrivateButton btn;4 @Override5 protected voidonCreate (Bundle savedinstancestate)6 {7 Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9BTN = (Button) This. Findviewbyid (R.ID.BTN1);Ten } One}
Second, Android five large layout
- Linear layout (linearlayout)
- Single Frame layout (framelayout)
- Absolute layout (absolutelayout)
- Table layout (tablelayout)
- Relative Layout (Relativelayout)
- Common Properties
//the component is located on the left side of the reference componentAndroid:layout_toleftof//the component is located to the right of the reference componentAndroid:layout_torightof//the component is above the referencing componentAndroid:layout_above//the component is below the reference componentAndroid:layout_below//whether the component aligns to the left side of the parent componentAndroid:layout_alignparentleft//whether the component is aligned to the right end of its parent componentAndroid:layout_alignparentright//whether the component aligns to the top of the parent componentAndroid:layout_alignparenttop//whether the component aligns to the bottom of the parent componentAndroid:layout_alignparentbottom//whether the component is centered relative to the parent componentandroid:layout_centerinparent//whether the component is centered horizontallyAndroid:layout_centerhorizontal//whether the component is vertically centeredandroid:layout_centervertical//the component weight/priority, which represents the proportion of space the control occupiesAndroid:layout_weight
Ps:layout_weight--Specifies the weight or precedence of the control, and the most obvious effect is the size of the space used in comparison to other controls.
Example:
1 Public classSecondactivityextendsActivity2 {3 Privatelinearlayout mlinearlayout;4 PrivateTextView Mtextview;5 PrivateButton mBtn;6 @Override7 protected voidonCreate (Bundle savedinstancestate)8 {9 Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_second); OneMlinearlayout =NewLinearLayout ( This); A mlinearlayout.setorientation (linearlayout.vertical); -Mlinearlayout.setbackgroundcolor (0xffff0000); - Setcontentview (mlinearlayout); theMtextview =NewTextView ( This); -Mtextview.settext ("Second Activity"); - //Add a child element to a view - Mlinearlayout.addview (mtextview); +MBtn =NewButton ( This); -Mbtn.settext ("Back"); + Mlinearlayout.addview (MBTN); AMbtn.setonclicklistener (NewButton.onclicklistener () at { - @Override - Public voidOnClick (View v) - { - //Create intent, data-passing media - FinalIntent i =NewIntent (secondactivity. This, Mainactivity.class); in //send the OS into another activity message - startactivity (i); to //change the current activity state to complete, i.e. destroy. +Secondactivity. This. Finish (); - } the }); * $ }Panax Notoginseng}
Third, attribute differences
When you set control properties, you often see android:layout_width and Android:width, so what is the difference between the two? The "Layout_" prefix, which refers to the relationship of the parent control to the entire control, without the "layout_" prefix, is the textual content of the control itself. For example: Layout_gravity refers to the alignment in the parent control, and gravity is the alignment of the text content of the control itself. The Layout_margin is the same level of control spacing as the accusation piece.
Android:layout_width Android:layout_height Android:layout_margin android:layout_gravity Android:text Android:textcolor android:gravityAndroid:width
- The difference between match_parent and wrap_parent
- Match_parent: Sets the view to the same size as its parent view. (Setting the layout of a control to match_parent will force the control to expand to fill as much space in the layout unit as possible.) )
- Wrap_parent: The view automatically resizes according to its content. (Setting a view to a size of wrap_content will force the view to expand to show the entire content.) )
Android layout Layouts