Android learning-LinearLayout linear layout and linearlayout Layout
LinearLayout linear Layout
LinearLayout is a linear layout. The components in the LinearLayout layout container are arranged one by one: you can not only control the horizontal arrangement of components, but also control the vertical arrangement of each component. Use the orientation attribute to set whether the linear arrangement is vertical or horizontal ).
The following uses XML layout and Java code layout as examples:
I. XML Layout
1. Create a blank Activity
2. Open the "res/layout/activity_main.xml" file and modify it to the following code.
(1) Part ①
<? Xml version = "1.0" encoding = "UTF-8">, Each XML document starts from the XML preface. The first line in the previous code is the XML preface. <? Xml version = "1.0">. This line of code indicates parsing according to the XML rule of version 1.0. Encoding = "UTF-8" indicates that the xml file adopts the UTF-8 encoding format. The encoding format can also be GB2312.
If you do not understand this, see the relevant XML documentation.
(2) Part ②
<LinearLayout ...... The linear layout manager is used.
(3) Section ③
Android: layout_width = "match_parent" android: layout_height = "match_parent" indicates that the layout manager width and height fill the entire screen width and height.
(4) Part 4
Android: orientation = "vertical" indicates that components in the layout manager are arranged vertically.
To use the horizontal direction, use 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 attribute of the three buttons to "match_parent ".
This attribute can have three values: wrap_content, match_parent, and fill_parent.
Wrap_content indicates the width Matching content. Simply put, it is the length of the button for the text.
Match_parent indicates that the width matches the parent content. If the container outside the button has multiple widths, the display is multiple widths.
Fill_parent is the same as match_parent. It is not recommended for later versions of android2.2.
The final result is as follows:
Ii. Java code Layout
We have learned how to use XML for the LinearLayout layout. Now let's take a look at how to use Java code to complete the same functions.
1. Open the "src/com. genwoxue. LinearLayout/MainActivity. java" file.
Enter the following code:
In the above Code, we will focus on analyzing the section with a light blue background.
(1) Part ①
Import packages related to LinearLayout, LayoutParams, And Button.
(2) Part ②
Create a linear layout manager and set the layout management width, height, and direction.
LinearLayout llLayout = new LinearLayout (this): Creates a linear layout manager;
LayoutParams lpPara = new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT): Create a layout parameter and the constructor sets the width and height. Used to set the width and height of the linear layout manager.
LlLayout. setOrientation (LinearLayout. VERTICAL): sets the layout manager to the VERTICAL direction.
(3) Section ③
LayoutParams btnPara = new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT): Create a layout parameter and the constructor sets the width and height. Set the width and height of the three buttons.
(4) Part 4
Create three buttons: btnFourth, btnFifth, and btnSixth. Set the text and layout parameters.
(5) Part ⑤
Add three buttons for the linear layout manager.
(6) Part 6
Super. addContentView (llLayout, lpPara): adds parameters for the layout manager and layout manager for the current activity.
2. Display Results