Table of contents
1. Create an Android application
Start the application to see the effect
5. Set the layout properties and view the effect
(4) Set the linear layout background
1. Create an Android application
Linear layout (LinearLayout) is a relatively common and simple layout method. In this layout, all child elements are arranged on the interface in vertical or horizontal order. If it is arranged vertically, each child element occupies a row, if it is arranged horizontally, each child element occupies a column. Linear layout can support layout style nesting to implement complex layout styles.
Inheritance Diagram
LinearLayout is a subclass of ViewGroup
common attributes
Attributes | meaning |
---|---|
layout_height | Height, unit: dp (wrap_content, match_parent) |
layout_weight | Width, unit: dp (wrap_content, match_parent) |
orientation | direction (vertical, horizontal) |
gravity | Alignment (left, right, center, top, bottom…) |
background | Background (color [color], picture [drawable], selector [selector]) |
weight | Specific gravity (used to carve up mobile phone screens) |
padding | padding(paddingLeft, paddingRight, paddingTop, paddingBottom) |
margin | Margins (marginLeft, marginRight, marginTop, marginBottom) |
1. Create an Android application
Create an Android application based on Empty Activity
a template - LinearLayoutDemo
Click the [Finish] button
2. Main layout resource file
Change the constraint layout to a linear layout and delete the default label
add two buttons
3. String resource file
Open the string resource file strings.xml to modify the theme
Start the application to see the effect
Found two buttons placed horizontally, in the upper left corner of the window
5. Set the layout properties and view the effect
(1) Set the linear layout direction orientation
property
(2) Set the inner margin of the linear layoutpadding (paddingTop, paddingBottom, paddingLeft, paddingRight)
(3) Set the linear layout alignment gravity
(left, center, right, top, bottom can be combined to form a variety of alignments) set the upper right alignment - right|top
Delete the right margin of [Button 1]
Set center alignment - center
Set Bottom Left Alignment - left|bottom
Set top center alignment - center
(4) Set the linear layout background
Set background color (takes color variable)
Set the background color (takes a color constant)
set background image
Set the background selector
Add a linear layout, set a custom border
Realize border gradient effect
nested linear layout
1. Create an Android application
Empty Activity
Create android app based on -NestedLinearLayout
2. Prepare picture material
Copy a few small pictures to the res/drawable directory
3. Main layout resource file
Main layout resource file - activity_main.xml
Change the constraint layout to a linear layout, add three linear layouts, and divide the mobile phone screen vertically according to the ratio of 1:2:3
Modify the layout and see the effect
Add three image views arranged horizontally to the first layout
Add a horizontal linear layout to the second layout, add four buttons inside
Add an edit box to the second layout
Add three layouts and add buttons to the third layout
operation result