Linearlayout of Android layout manager

Source: Internet
Author: User

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:

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.