Layout layouts LinearLayout Linear layout

Source: Internet
Author: User

Original Address http://blog.csdn.net/jianghuiquan/article/details/8295714

From Hello world!, we have been learning in a layout, of course, for the basic content of learning, there is no problem! But--

UI design is also very important in Android development, when users use an app, the first thing to feel is not whether the software is powerful, but whether the interface design is pleasing, the user experience is good. It can also be said that there is a good interface design to attract the user's use, in order to allow more users to experience the powerful software features.

Then, several common layouts in Android are critical. Each layout can be used alone or nested, and we should be flexible in practical applications.

LinearLayout Linear layout

LinearLayout is a way of laying out a linetype. The components in the LinearLayout layout container are arranged one after the other: not only can the components be controlled horizontally, but also the components can be arranged vertically. The Orientation property sets whether the direction of the linear arrangement is vertical (vertical) or horizontal (horizontal).

  

The following are examples of the XML layout and the Java code layout, respectively:

First, the layout of XML mode

1. Create a blank activity

  

2. Open the "res/layout/activity_main.xml" file and modify it to the following code.

  

(1) Section ①

<?xml version= "1.0" encoding= "Utf-8"; each XML document starts with the XML prologue, and the first line in the preceding code is the XML preamble, <?xml version= "1.0" >. This line of code means parsing according to the 1.0 version of the XML rule. encoding = "Utf-8" indicates that the XML file is in UTF-8 encoded format. The encoding format can also be GB2312.

If you're not sure about this, see the relevant XML documentation.

(2) Section ②

<linearlayout ... Represents the use of the Linetype layout manager.

(3) Section ③

Android:layout_width= "Match_parent" android:layout_height= "match_parent" means that the layout manager width and high charge will fill the entire screen width and height.

(4) Section ④

Android:orientation= "Vertical" indicates that the components in the layout manager are arranged vertically.

If you want to use horizontal orientation: horizontal.

3, insert three buttons.

  

4. Open the "res/layout/activity_main.xml" file and modify it to the following code.

  

Set the Android:layout_width property of the 3 buttons to: "Match_parent".

This property can have three values: Wrap_content, Match_parent, Fill_parent.

Wrap_content means that the width matches the content, in short, how long the text has a button.

Match_parent means that the width matches the parent content, and how wide the container outside the button appears.

Fill_parent and Match_parent are the same, android2.2 is not recommended after use.

The final results are as follows:

  

Ii. layout of Java code mode

Now that we've learned to use XML for linearlayout layouts, let's learn how to do the same with Java code.

1. Open the "Src/com.genwoxue.linearlayout/mainactivity.java" file.

Then enter the following code:

  

In the above code, we focus on a light blue background section.

(1) Section ①

Imports packages related to LinearLayout, layoutparams, and button.

(2) Section ②

Create a linear layout manager, and set the layout management width and height and orientation.

LinearLayout lllayout=new LinearLayout (This): Create a linear layout manager;

Layoutparams lppara=new Layoutparams (layoutparams.match_parent,layoutparams.match_parent): Creates a layout parameter, and the constructor sets the width and height. Lets you set the linear layout manager width and height.

Lllayout.setorientation (linearlayout.vertical): Sets the layout manager to a vertical orientation.

(3) Section ③

Layoutparams btnpara=new Layoutparams (layoutparams.match_parent,layoutparams.wrap_content): Creates a layout parameter, and the constructor sets the width and height. Used to set the width and height of three buttons.

(4) Section ④

Create 3 buttons: Btnfourth, Btnfifth, Btnsixth, and set their text and layout parameters.

(5) Section ⑤

Add 3 buttons to the linear layout manager.

(6) Section ⑥

Super.addcontentview (Lllayout, Lppara): Adds a layout manager and a parameter object for the layout manager for the current activity.

2. Display effect

  

Layout layouts LinearLayout Linear layout

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.