Tengo un problema con LinearLayout en Android. Tengo cuatro botones. Cada botón tiene un tamaño fijo, pero el texto puede variar en longitud.LinearLayout: no se alinea verticalmente
Mi problema es que no se alinean con la parte superior de cada uno. Se ve que se alinean con la parte superior del texto dentro de cada botón que cambia dependiendo de la cantidad de líneas que hay dentro del botón (Ver imagen).
Además, quiero seguir usando LinearLayout, ya que eventualmente usaré el código para agregar botones basados en datos de una base de datos.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="Line1 Line2" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
<Button android:text="Line1 Line2 Line3" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
<Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
<Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
</LinearLayout>
</LinearLayout>
EDIT: RESPUESTA (No se puede responder a mi propia pregunta):
Ok, acabo de encontrar la respuesta por mí mismo. Tienes que agregar android: baselineAligned = "false" a LinearLayout o cualquier otro control similar que pueda mostrar el mismo comportamiento.
También puede solucionar esto en el diseñador de IU con el botón denominado "Alternar alineación de línea base".
Así que el código resultante es:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:baselineAligned="false" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="Line1 Line2" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
<Button android:text="Line1 Line2 Line3" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
<Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
<Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
</LinearLayout>
</LinearLayout>
Gracias por el consejo sobre android: baselineAligned = "false", solucionó mi problema también :-) –