Android User Interface Design: Linear layout (1)

Source: Internet
Author: User

Understanding the layout is very important for a good Android program design. In this tutorial, you will learn all about linear layout, which vertically or horizontally organizes user interface controls or gadgets on the screen. Properly used, linear layout can be used as the basic layout. Based on this layout, many interesting Android user interfaces can be designed.

What is linear layout?

Linear layout is one of the simplest layout types that Android Developers use most. developers use it to organize controls on your user interfaces. The linear layout functions like its name: it organizes controls in a vertical or horizontal form. When the layout direction is set to vertical, all child controls in the layout are organized in the same column. When the layout direction is set to normal, all child controls are organized in one row.

Linear layout can be defined in the XML layout resource file, or dynamically defined using Java code in the program.

Shows the linear layout of seven TextView controls. This linear layout direction is set to vertical, so that each TextView control is displayed in a column. The text attribute of each TextView control is a color value, and the background color is the color. By setting the layout_width attribute of the control to fill_parent, each control is stretched to the screen width.

 

Use XML layout resources to define linear Layout

The most convenient and maintainable way to design a program user interface is to create XML layout resources. This method greatly simplifies the UI design process. It moves the layout of many static creation and user interface controls and the definition of control attributes to XML, rather than writing code.

XML layout resources must be stored in/res/layout of the project directory. Let's take a look at the Rainbow linear layout described in the previous section. This screen is basically a vertical linear layout set to fill the entire screen, which is achieved by setting its layout_width and layout_height attributes to fill_parent. Name it/res/layout/rainbow. xml as appropriate. The XML definition is as follows:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.    
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  4.    
  5. android:layout_width="fill_parent" android:layout_height="fill_parent" 
  6.    
  7. android:orientation="vertical"> 
  8.    
  9. <TextView android:text="RED" android:id="@+id/TextView01" 
  10.    
  11. android:layout_height="wrap_content" android:background="#f00" 
  12.    
  13. android:layout_width="fill_parent" android:layout_weight=".14" 
  14.    
  15. android:gravity="center" android:textColor="#000"></TextView> 
  16.    
  17. <TextView android:text="ORANGE" android:id="@+id/TextView02" 
  18.    
  19. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  20.    
  21. android:layout_weight=".15" android:background="#ffa500" 
  22.    
  23. android:gravity="center" android:textColor="#000"></TextView> 
  24.    
  25. <TextView android:text="YELLOW" android:id="@+id/TextView03" 
  26.    
  27. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  28.    
  29. android:layout_weight=".14" android:background="#ffff00" 
  30.    
  31. android:gravity="center" android:textColor="#000"></TextView> 
  32.    
  33. <TextView android:text="GREEN" android:id="@+id/TextView04" 
  34.    
  35. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  36.    
  37. android:layout_weight=".15" android:background="#0f0" android:gravity="center" 
  38.    
  39. android:textColor="#000"></TextView> 
  40.    
  41. <TextView android:text="BLUE" android:id="@+id/TextView05" 
  42.    
  43. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  44.    
  45. android:layout_weight=".14" android:background="#00f" android:gravity="center" 
  46.    
  47. android:textColor="#fff"></TextView> 
  48.    
  49. <TextView android:text="INDIGO" android:id="@+id/TextView06" 
  50.    
  51. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  52.    
  53. android:layout_weight=".14" android:background="#4b0082" 
  54.    
  55. android:gravity="center" android:textColor="#fff"></TextView> 
  56.    
  57. <TextView android:text="VIOLET" android:id="@+id/TextView07" 
  58.    
  59. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  60.    
  61. android:layout_weight=".14" android:background="#ee82ee" 
  62.    
  63. android:gravity="center" android:textColor="#000"></TextView> 
  64.    
  65. </LinearLayout> 

You may notice that every sub-control in this linear layout has many interesting attributes, including a property called layout_weight. We will talk about it later.

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.