- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Layout management in Android
In this chapter of the Android development tutorial we will talk about layout management. widgets.
When we design the user interface of our application, we decide what components we will use and how we will organise those components in the application. To organise our components, we use specialised non visible objects called layout managers.
There are several layout managers in Android. A LinearLayout
lines up its views in one row or column. A FrameLayout
is a simple layout manager used to display one view. A RelativeLayout
is a layout manager in which the views are positioned in relation to each other or to the parent. The most powerful layout manager is the GridLayout
manager. It arranges the views in a grid.
Showing an image with FrameLayout
The first example shows an image using the FrameLayout manager.
$ ls res/drawable-hdpi/ ic_launcher.png zamok.jpg
Depending on a android virtual device we are using, we put the image in the corresponding subdirectory of the res directory.
main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="top" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:layout_height="match_parent" android:layout_width="match_parent" android:src="@drawable/zamok" /> </FrameLayout>
In the FrameLayout
manager, we put one ImageView
.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="top" android:layout_width="wrap_content" android:layout_height="wrap_content" >
The FrameLayout
is big enough to display the ImageView
by setting the layout width and height to wrap_content
. It is pushed to the top using the layout_gravity
attribute.
<ImageView android:layout_height="match_parent" android:layout_width="match_parent" android:src="@drawable/zamok" />
The ImageView
displays an image. The image is located in a subdirectory of the res directory.

A row of buttons
In the example we create a row of four buttons.
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button1" /> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button2" /> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button3" /> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button4" /> </LinearLayout>
We have a horizontal LinearLayout
. In this layout, we add four buttons.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" >
We create a horizontal LinearLayout manager. The width and height of the layout match the parent which means that it fills the entire screen.
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button1" />
Each of the four buttons use the wrap_content
property. They are then just big enough to display their content.

A row of buttons II
In the third example of this chapter, we show how to programmatically create a row of buttons with a LinearLayout
manager.
MainActivity.java
package com.zetcode.btnrow2; import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.widget.LinearLayout; public class ButtonRow2 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initUI(); } public void initUI() { Button btn1 = new Button(this); btn1.setText("Button"); Button btn2 = new Button(this); btn2.setText("Button"); Button btn3 = new Button(this); btn3.setText("Button"); Button btn4 = new Button(this); btn4.setText("Button"); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.HORIZONTAL); ll.addView(btn1); ll.addView(btn2); ll.addView(btn3); ll.addView(btn4); setContentView(ll); } }
Four buttons are placed in a horizontal LinearLayout
. We will not use the layout XML file in this sample.
Button btn1 = new Button(this); btn1.setText("Button");
A Button
widget is created. The text is set for the button with the setText()
method.
LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.HORIZONTAL);
A horizontal LinearLayout
is created.
ll.addView(btn1); ll.addView(btn2); ll.addView(btn3); ll.addView(btn4);
Buttons are added to the layout manager.
setContentView(ll);
The linear layout manager is set to be the content view of the activity.
A column of buttons
We use the FrameLayout
and the LinearLayout
managers to create a column of buttons centered on the screen.
main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" /> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" /> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" /> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" /> </LinearLayout> </FrameLayout>
A LinearLayout manager with four buttons is placed in the FrameLayout manager.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" >
The FrameLayout does not occupy all the available space. It is just big enough to take all the four buttons. And therefore we can use the layout_gravity
attribute to center the LinearLayout and its four buttons.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" >
A vertical LinearLayout
is created.

RelativeLayout
RelativeLayout
lets child views specify their position relative to the parent view or to each other. The views are referenced by their ids.
main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/etId" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_sendId" android:layout_below="@+id/etId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" /> <Button android:id="@+id/btn_clearId" android:layout_below="@+id/etId" android:layout_toRightOf="@+id/btn_sendId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" /> </RelativeLayout>
The XML code displays an EditText
with two buttons.
<EditText android:id="@+id/etId" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" />
The EditText
will be stretched from right to left by setting the android:layout_width
to android:match_parent
. The widget will be high enough to show its contents. We specify some gap between the widget and the border of the screen with android:layout_marginTop
property.
<Button android:id="@+id/btn_sendId" android:layout_below="@+id/etId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" />
The Send button widget will be placed below the EditText
widget. To accomplish this, we use the android:layout_below
property. Note that we reference the id of the widget that we relate to.
<Button android:id="@+id/btn_clearId" android:layout_below="@+id/etId" android:layout_toRightOf="@+id/btn_sendId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" />
The Clear button is placed below the EditTex
t widget and to the right of the Send button. We accomplish this by two properties. The android:layout_below
and the android:layout_toRightOf
property.

Grid
A GridLayout
manager places its children in a rectangular grid. The grid consists of row and columns. The intersections of rows and columns are cells. Each cell is referenced by its index. A view in a grid can occupy one or more cells. The gravity
is a property that specifies how a view should be placed in its group of cells.
main.xml
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:text="(0, 0)" android:layout_row="0" android:layout_column="0" /> <Button android:layout_row="0" android:layout_column="1" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" /> <Button android:text="(0, 3)" android:layout_row="0" android:layout_column="3" /> <Button android:text="(0, 4)" android:layout_row="0" android:layout_column="4" /> <Button android:layout_row="1" android:layout_column="0" android:layout_rowSpan="3" android:layout_columnSpan="5" android:layout_gravity="fill" /> <Button android:text="Center" android:layout_row="4" android:layout_column="0" android:layout_columnSpan="5" android:layout_gravity="center_horizontal" /> <Button android:text="Right" android:layout_row="5" android:layout_column="0" android:layout_columnSpan="5" android:layout_gravity="right" /> </GridLayout>
In the example we put a few buttons in a GridLayout. We show how a button can stretch over several cells.
<Button android:text="(0, 0)" android:layout_row="0" android:layout_column="0" />
Using the layout_row
and layout_column
properties, we place a button at top-left cell. The indeces start from zero.
<Button android:layout_row="0" android:layout_column="1" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" />
This button will span two columns. The layout_gravity
property will cause the button to fill the two columns.
<Button android:layout_row="1" android:layout_column="0" android:layout_rowSpan="3" android:layout_columnSpan="5" android:layout_gravity="fill" />
This button will span three rows and five columns.
<Button android:text="Center" android:layout_row="4" android:layout_column="0" android:layout_columnSpan="5" android:layout_gravity="center_horizontal" />
A view may not occupy all the space that was allotted to it. This button is horizontally centered within five columns.

In this chapter of the Android development tutorial we worked with layout management.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论