Getting started with Android _ Common Code Layout

Source: Internet
Author: User

 

1. Linear layout:
Linear layout is one of the most common classes in all la s and is also the parent class of the radiogroup, tabwidget, tablelayout, tablerow, and zoomcontrols classes. Linearlayout can arrange its child elements in a vertical or horizontal line (if no direction is set, the child elements are arranged in the vertical direction by default ).

For example:

Java code:
<? XML version = "1.0" encoding = "UTF-8"?>

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

>

<! --

Android: id -- specify the corresponding ID for the control

Android: Text -- specify the text displayed in the control. Note that strings in the strings. xml file should be used as much as possible here.

Android: grimace -- specifies the basic position of the control, such as center and right.

Android: textsize -- specifies the font size in the control.

Android: background -- specifies the background color used by the control. The RGB naming method is used.

Android: width -- specify the width of the control

Android: height -- specify the height of the control

Android: padding * -- specifies the padding of the control, that is, the content in the control.

Android: layout_weight -- weight ratio between controls

Android: sigleline -- if set to true, the control content is displayed in the same row.

-->

<Textview

Android: Id = "@ + ID/firsttext"

Android: text = "one row, one row"

Android: gravity = "center_vertical"

Android: textsize = "35pt"

Android: Background = "# aa0000"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: paddingleft = "10dip"

Android: paddingtop = "20dip"

Android: paddingright = "30dip"

Android: paddingbottom = "40dip"

Android: layout_weight = "1"

Android: singleline = "true"/>

<Textview

Android: Id = "@ + ID/secondtext"

Android: text = "second line"

Android: gravity = "center_vertical"

Android: textsize = "15pt"

Android: Background = "# tianaa"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"/>

</Linearlayout>
2. Relative layout: relativelayout
Relativelayout allows child elements to specify their positions relative to their parent or sibling elements, which is one of the most common layout methods in actual layout. It has a lot of flexibility. Of course, there are also many attributes, and the operation is difficult. There is also a high possibility of conflicts between attributes. When using relative layout, more tests are required.

For example:

Java code:
<? XML version = "1.0" encoding = "UTF-8"?>

<! --

Android: layout_above: Move the control's bottom to the control with the given ID

Android: layout_below: place the top of the control under the control with the given ID

Android: layout_toleftof align the right edge of the control with the left edge of the control with the given ID

Android: layout_torightof align the left edge of the control with the right edge of the control with the given ID

 

Android: layout_alignbaseline the baseline of the control is aligned with the baseline of the control with the given ID

Android: layout_alignbottom: Compares the bottom edge of the control with the bottom edge of the given ID control.

Android: layout_alignleft: Align the left edge of the control with the given ID

Android: layout_alignright: Align the right edge of the control with the right edge of the given ID Control

Android: layout_aligntop: aligntop

 

 

Android: alignparentbottom if the value is true, the bottom of the control is aligned with the bottom of the parent control.

Android: layout_alignparentleft if the value is true, the left side of the control is aligned with the left side of the parent control.

Android: layout_alignparentright if the value is true, the right side of the control is aligned with the right side of the parent control.

Android: layout_alignparenttop if the value is true, the top of the space is aligned with the top of the parent control.

 

Android: layout_centerhorizontal

Android: layout_centerinparent if the value is true, the control is centered in the horizontal and vertical directions of the parent control.

Android: layout_centervertical if the value is true, the control will be centered in the vertical direction

-->

<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent">

<Textview

Android: Id = "@ + ID/label"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "type here:"/>

<Edittext

Android: Id = "@ + ID/entry"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: Background = "@ Android: drawable/editbox_background"

Android: layout_below = "@ ID/label"/>

<Button Android: Id = "@ + ID/OK"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_below = "@ ID/entry"

Android: layout_alignparentright = "true"

Android: layout_marginleft = "10dip"

Android: text = "OK"/>

<Button Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_toleftof = "@ ID/OK"

Android: layout_aligntop = "@ ID/OK"

Android: text = "cancel"/>

</Relativelayout>
3. tablelayout
Used with tablerow, similar to the table in HTML.

For example:

Java code:

<? XML version = "1.0" encoding = "UTF-8"?>

 

<Tablelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: stretchcolumns = "1">

<Tablerow>

 

<Textview

Android: layout_column = "1"

Android: text = "open ..."

Android: padding = "3dip"/>

 

<Textview

Android: text = "Ctrl-o"

Android: gravity = "right"

Android: padding = "3dip"/>

</Tablerow>

<Tablerow>

 

 

<Textview

Android: layout_column = "1"

Android: text = "Save ..."

Android: padding = "3dip"/>

 

<Textview

Android: text = "Ctrl-s"

Android: gravity = "right"

Android: padding = "3dip"/>

</Tablerow>

 

<Tablerow>

<Textview

Android: layout_column = "1"

Android: text = "Save ..."

Android: padding = "3dip"/>

 

<Textview

Android: text = "Ctrl-shift-s"

Android: gravity = "right"

Android: padding = "3dip"/>

</Tablerow>

 

<View

Android: layout_height = "2dip"

Android: Background = "# ff909090"/>

<Tablerow>

<Textview Android: text = "*" Android: padding = "3dip"/>

<Textview Android: text = "import..." Android: padding = "3dip"/>

</Tablerow>

<Tablerow>

<Textview Android: text = "*" Android: padding = "3dip"/>

<Textview Android: text = "Export..." Android: padding = "3dip"/>

<Textview Android: text = "Ctrl-e" Android: gravity = "right" Android: padding = "3dip"/>

</Tablerow>

 

<View Android: layout_height = "2dip" Android: Background = "# ff909090"/>

<Tablerow>

<Textview Android: layout_column = "1" Android: text = "exit" Android: padding = "3dip"/>

 

</Tablerow>

 

</Tablelayout>
 

4. Switch the tabwidget
Inherits tabactivity to implement tag switching.
For example:

Java code:
<? XML version = "1.0" encoding = "UTF-8"?>

<Tabhost xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Id = "@ Android: ID/tabhost"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent">

<Linearlayout

Android: Orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent">

<Tabwidget

Android: Id = "@ Android: ID/tabs"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"/>

<Framelayout

Android: Id = "@ Android: ID/tabcontent"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent">

<Textview

Android: Id = "@ + ID/textview1"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: text = "this is a tab"/>

<Textview

Android: Id = "@ + ID/textview2"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: text = "this is another tab"/>

<Textview

Android: Id = "@ + ID/textview3"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: text = "this is a third tab"/>

</Framelayout>

</Linearlayout>

</Tabhost>
 

Other la s:

 

1. Frame layout framelayout:

Is the simplest layout object. All the display objects in the screen will be fixed in the upper left corner of the screen and cannot be specified. However, multiple display objects are allowed, but the last one will directly overwrite the previous one, blocks some or all of the preceding components.

For example:

 

Java code:

<? XML version = "1.0" encoding = "UTF-8"?>

<Framelayout

Xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent">

<Textview

Android: text = "big"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: textsize = "50pt"/>

<Textview

Android: text = "Middle"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: textsize = "20pt"/>

<Textview

Android: text = "small"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: textsize = "10pt"/>

</Framelayout>

2. Absolute layout: absolutelayout

Absolute positioning absolutelayout can also be called the coordinate layout, which can directly point to the absolute position of the stator element. This layout is simple and straightforward, but because of the large differences in the cell phone screen size, the adaptability to absolute positioning is poor. The display position varies depending on the resolution of the screen.

For example:

 

Java code:

<? XML version = "1.0" encoding = "UTF-8"?>

<Absolutelayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

 

<Edittext

Android: text = "welcome to Mr Wei's blog"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"/>

 

<Button

Android: layout_x = "250px" // sets the X coordinate of the button.

Android: layout_y = "40px" // sets the Y coordinate of the button.

Android: layout_width = "70px" // you can specify the width of a button.

Android: layout_height = "wrap_content"

Android: text = "button"/>

 

</Absolutelayout>

 

 

 

In my personal statement, I think this article is very expensive, so I collected it!
From: http://my.oschina.net/cathleencheng/blog/17691

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.