A layout defines the visual structure for a user interface, such as the UI for an activity, fragment or app widget.
An Android layout defines everything the user can see and touch. A layout is made up of View
(like buttons and text) and ViewGroup
(like lists, tables, or more Views) objects, all combined to make a View Hierarchy:
(from Android's UI Overview)
Designing a Layout:
You can create your layout in any combination of these two ways:
Declare UI elements in XML.
Android provides a straightforward XML vocabulary that corresponds to theView
classes and subclasses, such as those for widgets and layouts.Instantiate layout elements at runtime.
Your application can createView
andViewGroup
objects (and manipulate their properties) programmatically.
Common Layouts:
Each subclass of the ViewGroup class provides a unique way to display the views you nest within it. Below are some of the more common layout types that are built into the Android platform.
- Linear Layout: A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar if the length of the window exceeds the length of the screen.
- Relative Layout: Enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).
- Constraint Layout: Allows positioning children relative to each other and the parent. But also offers other powerful positioning and sizing strategies, including horizontal/vertical child "chains" with custom spacing/weighting, arbitrary horizontal/vertical "guidelines," and custom child size aspect ratios.
- Web View: Displays web pages.
- Frame layout: FrameLayout is designed to block out an area on the screen to display a single item.
- Grid View: GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.
Useful Links:
- Android Developer Guides:
- Learn the basics with Declaring a Layout
- Advanced Design Topics
- Android's Blog about layout Tips and tricks
- Android Layouts
- Hierarchy Viewer: This tool allows you to debug and optimize your layout.