Android Program Development: (5) screen components-5.2 LinearLayout linear Layout

Source: Internet
Author: User

LinearLayout organizes a view into one row or one column. Child views can be arranged vertically or horizontally. To learn how LinearLayout works, first consider the typical mail. xml file.

 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: orientation = "vertical">
  6. <TextView
  7. Android: layout_width = "fill_parent"
  8. Android: layout_height = "wrap_content"
  9. Android: text = "@ string/hello"/>
  10. </LinearLayout>
In main. xnl, the root node element is <LinearLayout>, which contains a <TextView> element. The <LinearLayout> element controls the order of views it contains.

Each View and ViewGroup have some common attributes. For example:

 

Attribute Description
Layout_width Specify the width of the View or ViewGroup.
Layout_height Height of a View or ViewGroup
Layout_marginTop Extra space above the View or ViewGroup
Layout_marginBottom Specify additional space under the View or ViewGroup
Layout_marginLeft Extra space on the left side of the View or ViewGroup
Layout_marginRight Extra space on the right of the View or ViewGroup
Layout_gravity Specify the position of the child views in the View or ViewGroup.
Layout_weight Extra space size assigned to View or ViewGroup
Layout_x X coordinate of the View or ViewGroup
Layout_y Y coordinate of View or ViewGroup

For example, the <TextView> element uses the fill_parent constant, so the width fills the width of the layout where the entire element is located. Its height is set to the constant wrap_content, which means that its height will be the same as its layout. If you do not want <TextView> to occupy the entire screen, you can set the layoutt_width element to wrap_content, just like this:

 
  1. <TextView
  2. Android: layout_width = "wrap_content"
  3. Android: layout_height = "wrap_content"
  4. Android: text = "@ string/hello"/> <span style = "font-family: Arial, Helvetica, sans-serif;">
  5. </Span> www.2cto.com

The preceding Code sets the TextView width to the same length as the text contained in the TextView.

See the following layout:

 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: orientation = "vertical">
  6. <TextView
  7. Android: layout_width = "100dp"
  8. Android: layout_height = "wrap_content"
  9. Android: text = "@ string/hello"/>
  10. <Button
  11. Android: layout_width = "160dp"
  12. Android: layout_height = "wrap_content"
  13. Android: text = "Button"/>
  14. </LinearLayout>
Here, the TextView and Button width are set to a fixed value.
From manoel's column

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.