LINEAR LAYOUT
Desarrollo de Aplicaciones Móviles con Android Studio
Para la demostración sobre la funcionalidad del LinearLayout, crearemos un proyecto en Android Studio con la siguiente config uracion:
Aplication Name : Ejm02
Module Name : Ejm02
Package Name : com.example.ejm02
Minimum requiered SDK : API 26 Android 8.0
Target SDK : API 28 Android 9.0
Language Level : API 28 Android 9.0
01. Visualizamos el archivo MainActivity.java donde se ubica la clase principal del proyecto. Luego reemplazamos por el
siguiente código.
package com.example.ejm02;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Desarrollo de Aplicaciones Móviles con Android Studio
02. Posteriormente visualizamos el archivo activity_main.xml, donde se puede observar el tipo de layout por defecto.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Desarrollo de Aplicaciones Móviles con Android Studio
03. Modificamos el archivo activity_main.xml, y luego adicionamos botones al layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="@+id/btn01"
android:text="Boton 01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn02"
android:text="Boton 02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn03"
android:text="Boton 04"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn05"
android:text="Boton 05"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Desarrollo de Aplicaciones Móviles con Android Studio
04. Como se puede observar en código se ha modificado, el layout original fue reemplazado por el LinearLayout y se ha
adicionado cinco botones.
05. La vista previa del proyecto se muestra.
06. Como observamos los controles se han distribuido de forma horizontal, debido a la configuración de la orientación del
layout.
Desarrollo de Aplicaciones Móviles con Android Studio
07. Ahora editaremos nuevamente el archivo activity_main.xml, para adicionar la propiedad layout_gravity.
08. Se muestra el archivo xml modificado.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn01"
android:text="Boton 01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
/>
<Button
android:id="@+id/btn02"
android:text="Boton 02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn03"
android:text="Boton 03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_gravity="right"
/>
</LinearLayout>
Desarrollo de Aplicaciones Móviles con Android Studio
09. En la vista previa del proyecto se muestra el posicionamiento de los controles y su altura. El atributo layout_gravity
indica la posición del contenido, mientras que el atributo layout_weight especifica la distribución de espacio disponible.
En el ejemplo anterior, los tres botones ocupan alrededor del 16,6% (1 / (1 + 2 + 3) * 100), 33,3% (2 / (1 + 2 + 3) * 100), y
50% (3 / (1 + 2 + 3) * 100) de la altura disponible, respectivamente.
10. Vista previa del archivo .xml