All controls in Android inherit from the view. The view and its subclass attributes can be configured in the layout XML file or inCode.
Viewgroup is a subclass of view, but can act as a container for other controls. The child control of viewgroup can be either a common view or a viewgroup. It adopts the composite design mode. The layout manager is inherited from the viewgroup.
1. Linear layout linearlayout
During layout, you can set the weight parameter of the control to control the relative size of each control in the container.
Attribute name |
Method |
Description |
Android: Orientation |
Setorientation (INT) |
Horizontal/vertical |
Android: gravity |
Setgravity (INT) |
Sets the control alignment mode. Options include top, buttom, left, right, center_vertical, center_horizontal, center, fill_vertical (vertically stretched and filled container), fill_horizontal, and fill. |
Linear_layout.xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android =" Http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: Orientation = "horizontal" Android: Id = "@ + ID/lla1">
<Button Android: Id = "@ + ID/Bt1" Android: text = "button1"
Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/>
<Button Android: Id = "@ + ID/bt2" Android: text = "button1"
Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/>
<Linearlayout xmlns: Android =" Http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: Orientation = "vertical" Android: Id = "@ + ID/lla2">
<Button Android: Id = "@ + ID/bt3" Android: text = "button3"
Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/>
<Button Android: Id = "@ + ID/bt4" Android: text = "button4"
Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/>
</Linearlayout>
</Linearlayout>
Add the following code to mainactivity:
Public class mainactivity extends activity {
/** Called when the activity is first created .*/
Int COUNT = 0;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. linear_layout );
Button Bt1 = (button) findviewbyid (R. Id. Bt1 );
Bt1.setonclicklistener (New button. onclicklistener (){
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Linearlayout lla2 = (linearlayout) findviewbyid (R. Id. lla2 );
String MSG = mainactivity. This. getresources (). getstring (
R. String. Button); // obtain the resource
Button added = new button (mainactivity. This );
Added. settext (MSG + (count ++ ));
Lla2.addview (added );
}
});
}
}
When you click button1, the button will be added to the second linearlayout: