Android XML layout and sub-view scaled layout

Source: Internet
Author: User

First of all, according to the directory structure of the program roughly analyzed:
res/layout/This directory is the layout of the XML file, the general default is Main.xml
Res/values/This directory holds a bunch of constant XML files
res/drawable/is storing some pictures or something, and of course the icons are here.
Below is mainly the layout of the XML file to do an introduction, by the way, also summarize the method:
• The beginning of the file
<?xml version= "1.0" encoding= "Utf-8"?>
This is a description of the XML version and the character encoding
• Immediately following the critical section:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
</LinearLayout>
The beginning of the "LinearLayout" is the layout of the way, there can be many kinds, the most commonly used should be linear, other layout methods, etc. in the following summary.
Next Android:layout_width (height) = "Wrap_content" is the width or absolute value of setting this part of the layout, which is, of course, set to the absolute value when the unit is marked.
• You can add controls between <linearlayout ...> and </LinearLayout>, such as adding a button control named Btn, and the text displayed on the button is "test!" and can be written like this:
<button id= "@+id/btn"
android:text= "test!"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
The name of the control is followed by the beginning ID, which is used when adding event listener, and the next few lines of Android:xxx are the properties of the set control, which are available in the Android document and do not need to be specifically recorded, and are generally available.
• One thing to note is that the layout method can be nested, a bit like the Java container, can be very convenient to the interface "heap" out.
A simple description of how the layout works:
Check out the Android document Discovery layout is really a lot, just list two things I think are more common:
· LinearLayout linear layout, either up or down, or around the addition of controls, very common;
· GridView Chinese translation comes in the grid layout, the control in order to fill in the sequence of each lattice is good, out of the interface will be very neat, more commonly used;
   The following are some of the specific layouts:
   @ <1>LinearLayout (linear layout) provides a model for the control to be horizontally vertically arranged, and you can control the relative size of individual controls in the layout by setting the weight layout parameters of the child controls. Horizontal (vertical) vertical (horizontal)
Fill-parent: Fills the entire screen, wrap-content: Just right for the size of the control's content
Alignment Gravity Value:
Top: Does not change size, position is placed on top of container
Bottom: Does not change size, position is placed at the bottom of the container
Left: does not change size, position is placed on container
Right: does not change size, position is placed on container
Center_vertical: Does not change size, position is placed in the vertical central part of the container
Center_horizontal: Does not change size, position is placed in the horizontal central part of the container
Center: does not change size, position is placed in the central part of the container's horizontal and vertical
Fill_vertical: If possible, the longitudinal extension can fill the container
Fiil_horizontal: If possible, the horizontal extension can fill the container
Fiil: If possible, vertical and horizontal extension fills the container
  @<2>absolutelayout (coordinate layout) allows child elements to specify accurate x/Y coordinate values and display them on the screen. (0, 0) is the upper-left corner, and when you move down or to the right, the coordinate value becomes larger. Absolutelayout does not have a page border, allowing elements to overlap each other (although not recommended). We don't usually recommend using absolutelayout unless you have a legitimate reason to use it because it makes the interface code too rigid that it might not work well on different devices.
Android:layout_x/layout_y= "56px" to determine the position of the control
   @<3>Relativelayout (relative layout) allows child elements to specify their location relative to other elements or parent elements (specified by ID). As a result, you can arrange two elements in the form of Right-aligned, or up or down, or centered on the screen. Elements are arranged sequentially, so if the first element is in the center of the screen, then the other elements relative to the element are arranged in the relative position of the center of the screen. If you use XML to specify this layout, the associated element must be defined before you can define it.
Android:layout_centerinparent, places the current control in the horizontal and vertical central part of the parent control Android:layout_centerhorizontal, placing the current control in the central part of the parent control's landscape
Android:layout_centervertival to place the current control in the central part of the parent control's portrait
Android:layout_alignparentleft to align the left and parent controls of the current control
Android:layout_alignparentright, aligns the right end of the current control with the right end of the parent control
Android:layout_alignparenttop to align the top of the current control with the top of the parent control
Android:layout_alignparentbottom to align the bottom of the current control with the bottom of the parent control
The above property can only set a value of type bool, "true" or "false"
Android:layout_below/layout_above/layout_toleftof/layout_torightof = "@id/" causes the current control to be placed below/above/to the left/right of the space given the ID
Android:layout_marginbottom/layout_marginleft/layo ut_marginright/layout_margintop= "30px" makes the current control bottom/left/right/ Empty the pixel space at the top of the
   @<4>Framelayout (single frame layout) is the simplest layout object. It is customized as a blank alternate area on your screen, after which you can populate a single object-for example, a picture you want to publish. All child elements will be pinned to the upper-left corner of the screen; You cannot specify a location for a child element in Framelayout. The latter child element will overwrite the padding directly above the previous child element, blocking them partially or completely (unless the latter element is transparent).

android:src=http://blog.soso.com/qz.q/the "@drawable/" property specifies the file location of the desired picture, you should also use ANDROID:SRC to specify the picture to display when you display the picture with ImageView
  @<5> Tablelayout (table layout) manages child controls in the form of rows and columns, each of which behaves as a TableRow object, TableRow can also add child controls
Android:collapsecolumns= "n" hides the column N of the TableRow inside the Tablelayout
Android:stretchcolumns= "n" to set column n as an extensible column
Android:shrinkcolumns= "n" to set column N to be a shrinking column
android:src=http://blog.soso.com/qz.q/the "@drawable/" property specifies the file location of the desired picture, you should also use ANDROID:SRC to specify the picture to display when you display the picture with ImageView


-------------- The previous section is for copying and pasting others--------------------

In an Android linear layout, the weight property is required to implement its child controls in a proportional arrangement:

??

The picture is 3 TextView plus a imageview where the proportions are 2:1:1:1

The first TextView occupies 2/5 of the total width of the remaining 1/5, when there is too much content in TextView, and the height is set to: Wrap_content will be divided into multiple lines, and when set to Fill_parent, some words are hidden.

To implement this layout: is included in all view LinearLayout, and to set Layaout_width to Fill_parent,oriantatioan set to horizontal, the layout_ of its child view Width is set to 0dip, and the set weight value is set by the percentage.

The code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/ll_listview"
android:orientation= "Horizontal"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:paddingtop= "10dip"
Android:paddingbottom= "10dip"
android:gravity= "Center"
>

<textview android:id= "@+id/name"
Android:layout_gravity= "Center_vertical"
Android:layout_width= "0dip"
android:layout_height= "Wrap_content"
android:text= "Cannon"
android:drawableleft= "@drawable/music_icon"
Android:layout_weight= "2"
Android:gravity= "left"
android:paddingleft= "5dip"
/>
<textview android:id= "@+id/time"
Android:layout_width= "0dip"
android:layout_height= "Wrap_content"
android:text= "12:01"
android:layout_weight= "1"
android:gravity= "Center"
/>
<imageview android:id= "@+id/difficult"
Android:layout_width= "0dip"
android:layout_height= "Wrap_content"
android:src= "@drawable/five_star"
android:layout_weight= "1"
android:gravity= "Center"
/>
<textview android:id= "@+id/record"
Android:layout_width= "0dip"
android:layout_height= "Wrap_content"
android:text= "20000"
android:layout_weight= "1"
android:gravity= "Center"
/>
</LinearLayout>

If you need to set the proportional allocation vertically, the corresponding height is odip.


First of all, according to the directory structure of the program roughly analyzed:
res/layout/This directory is the layout of the XML file, the general default is Main.xml
Res/values/This directory holds a bunch of constant XML files
res/drawable/is storing some pictures or something, and of course the icons are here.
Below is mainly the layout of the XML file to do an introduction, by the way, also summarize the method:
• The beginning of the file
<?xml version= "1.0" encoding= "Utf-8"?>
This is a description of the XML version and the character encoding
• Immediately following the critical section:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
</LinearLayout>
The beginning of the "LinearLayout" is the layout of the way, there can be many kinds, the most commonly used should be linear, other layout methods, etc. in the following summary.
Next Android:layout_width (height) = "Wrap_content" is the width or absolute value of setting this part of the layout, which is, of course, set to the absolute value when the unit is marked.
• You can add controls between <linearlayout ...> and </LinearLayout>, such as adding a button control named Btn, and the text displayed on the button is "test!" and can be written like this:
<button id= "@+id/btn"
android:text= "test!"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
The name of the control is followed by the beginning ID, which is used when adding event listener, and the next few lines of Android:xxx are the properties of the set control, which are available in the Android document and do not need to be specifically recorded, and are generally available.
• One thing to note is that the layout method can be nested, a bit like the Java container, can be very convenient to the interface "heap" out.
A simple description of how the layout works:
Check out the Android document Discovery layout is really a lot, just list two things I think are more common:
· LinearLayout linear layout, either up or down, or around the addition of controls, very common;
· GridView Chinese translation comes in the grid layout, the control in order to fill in the sequence of each lattice is good, out of the interface will be very neat, more commonly used;
   The following are some of the specific layouts:
   @ <1>LinearLayout (linear layout) provides a model for the control to be horizontally vertically arranged, and you can control the relative size of individual controls in the layout by setting the weight layout parameters of the child controls. Horizontal (vertical) vertical (horizontal)
Fill-parent: Fills the entire screen, wrap-content: Just right for the size of the control's content
Alignment Gravity Value:
Top: Does not change size, position is placed on top of container
Bottom: Does not change size, position is placed at the bottom of the container
Left: does not change size, position is placed on container
Right: does not change size, position is placed on container
Center_vertical: Does not change size, position is placed in the vertical central part of the container
Center_horizontal: Does not change size, position is placed in the horizontal central part of the container
Center: does not change size, position is placed in the central part of the container's horizontal and vertical
Fill_vertical: If possible, the longitudinal extension can fill the container
Fiil_horizontal: If possible, the horizontal extension can fill the container
Fiil: If possible, vertical and horizontal extension fills the container
  @<2>absolutelayout (coordinate layout) allows child elements to specify accurate x/Y coordinate values and display them on the screen. (0, 0) is the upper-left corner, and when you move down or to the right, the coordinate value becomes larger. Absolutelayout does not have a page border, allowing elements to overlap each other (although not recommended). We don't usually recommend using absolutelayout unless you have a legitimate reason to use it because it makes the interface code too rigid that it might not work well on different devices.
Android:layout_x/layout_y= "56px" to determine the position of the control
   @<3>Relativelayout (relative layout) allows child elements to specify their location relative to other elements or parent elements (specified by ID). As a result, you can arrange two elements in the form of Right-aligned, or up or down, or centered on the screen. Elements are arranged sequentially, so if the first element is in the center of the screen, then the other elements relative to the element are arranged in the relative position of the center of the screen. If you use XML to specify this layout, the associated element must be defined before you can define it.
Android:layout_centerinparent, places the current control in the horizontal and vertical central part of the parent control Android:layout_centerhorizontal, placing the current control in the central part of the parent control's landscape
Android:layout_centervertival to place the current control in the central part of the parent control's portrait
Android:layout_alignparentleft to align the left and parent controls of the current control
Android:layout_alignparentright, aligns the right end of the current control with the right end of the parent control
Android:layout_alignparenttop to align the top of the current control with the top of the parent control
Android:layout_alignparentbottom to align the bottom of the current control with the bottom of the parent control
The above property can only set a value of type bool, "true" or "false"
Android:layout_below/layout_above/layout_toleftof/layout_torightof = "@id/" causes the current control to be placed below/above/to the left/right of the space given the ID
Android:layout_marginbottom/layout_marginleft/layo ut_marginright/layout_margintop= "30px" makes the current control bottom/left/right/ Empty the pixel space at the top of the
   @<4>Framelayout (single frame layout) is the simplest layout object. It is customized as a blank alternate area on your screen, after which you can populate a single object-for example, a picture you want to publish. All child elements will be pinned to the upper-left corner of the screen; You cannot specify a location for a child element in Framelayout. The latter child element will overwrite the padding directly above the previous child element, blocking them partially or completely (unless the latter element is transparent).

android:src=http://blog.soso.com/qz.q/the "@drawable/" property specifies the file location of the desired picture, you should also use ANDROID:SRC to specify the picture to display when you display the picture with ImageView
  @<5> Tablelayout (table layout) manages child controls in the form of rows and columns, each of which behaves as a TableRow object, TableRow can also add child controls
Android:collapsecolumns= "n" hides the column N of the TableRow inside the Tablelayout
Android:stretchcolumns= "n" to set column n as an extensible column
Android:shrinkcolumns= "n" to set column N to be a shrinking column
android:src=http://blog.soso.com/qz.q/the "@drawable/" property specifies the file location of the desired picture, you should also use ANDROID:SRC to specify the picture to display when you display the picture with ImageView


-------------- The previous section is for copying and pasting others--------------------

In an Android linear layout, the weight property is required to implement its child controls in a proportional arrangement:

??

The picture is 3 TextView plus a imageview where the proportions are 2:1:1:1

The first TextView occupies 2/5 of the total width of the remaining 1/5, when there is too much content in TextView, and the height is set to: Wrap_content will be divided into multiple lines, and when set to Fill_parent, some words are hidden.

To implement this layout: is included in all view LinearLayout, and to set Layaout_width to Fill_parent,oriantatioan set to horizontal, the layout_ of its child view Width is set to 0dip, and the set weight value is set by the percentage.

The code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/ll_listview"
android:orientation= "Horizontal"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:paddingtop= "10dip"
Android:paddingbottom= "10dip"
android:gravity= "Center"
>

<textview android:id= "@+id/name"
Android:layout_gravity= "Center_vertical"
Android:layout_width= "0dip"
android:layout_height= "Wrap_content"
android:text= "Cannon"
android:drawableleft= "@drawable/music_icon"
Android:layout_weight= "2"
Android:gravity= "left"
android:paddingleft= "5dip"
/>
<textview android:id= "@+id/time"
Android:layout_width= "0dip"
android:layout_height= "Wrap_content"
android:text= "12:01"
android:layout_weight= "1"
android:gravity= "Center"
/>
<imageview android:id= "@+id/difficult"
Android:layout_width= "0dip"
android:layout_height= "Wrap_content"
android:src= "@drawable/five_star"
android:layout_weight= "1"
android:gravity= "Center"
/>
<textview android:id= "@+id/record"
Android:layout_width= "0dip"
android:layout_height= "Wrap_content"
android:text= "20000"
android:layout_weight= "1"
android:gravity= "Center"
/>
</LinearLayout>

If you need to set the proportional allocation vertically, the corresponding height is odip.


Android XML layout and sub-view scaled layout

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.