2012-05-16 15 views
5

Tengo una actividad con dos botones y una vista de texto en LinearLayout. Mi TextView está desplazado hacia abajo y el texto no cabe dentro de la caja. ¿Puedes explicar lo que está sucediendo? Creo que está relacionado con el relleno, y he leído varias discusiones sobre los peligros del relleno de TextView, pero eso no explica por qué el texto está cortado en la parte inferior.Android TextView desplazado hacia abajo

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="#800080"> 

    <Button 
     android:text="This" 
     android:background="@drawable/button_red" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    /> 
    <Button 
     android:text="That" 
     android:background="@drawable/button_green" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    /> 
    <TextView 
     android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this." 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#533f93" 
    /> 
</LinearLayout> 

Este código produce esta pantalla:

User interface

El púrpura es el LinearLayout, el azul es el TextView. Como puede ver, la parte superior de TextView está debajo de los botones y su parte inferior está debajo de LinearLayout. A medida que agrego texto a TextView, LinearLayout aumenta su altura de manera apropiada, pero como TextView está desplazado, siempre pierdo la parte inferior de la última línea.

me corrieron Jerarquía Visor y me dio esta estructura metálica:

Wireframe image from Hierarchy Viewer

que muestra la distancia al eje vertical en la parte superior, pero no alcanza la parte inferior de la Vista de Texto. La misma estructura metálica con las miradas LinearLayout seleccionados como este:

Wireframe image from Hierarchy Viewer - LinearLayout selected

Según Jerarquía Visor, la parte superior de los botones está en 0, pero la parte superior de la Vista de Texto es a las 7. He intentado varios arreglos , en su mayoría seleccionados de este sitio:

android:paddingTop="0dp" 
    android:background="@null" 
    android:includeFontPadding="false" 

Ninguno de estos ha solucionado el problema.

Respuesta

5

Conjunto android:baselineAligned propiedad de su LinearLayout a false.

De la documentación:

Cuando se establece en falsa, impide que el diseño de la alineación de sus niños líneas de base. Este atributo es particularmente útil cuando los niños usan valores diferentes para la gravedad. El valor predeterminado es true.

+0

Sí, eso funciona! No entiendo por qué el valor predeterminado es que LinearLayout empuje el texto más allá de sus bordes. – TomDestry

+1

Puede leer, por ejemplo, esta publicación para comprender qué significa [referencia básica] (https://groups.google.com/d/topic/android-developers/1gPy9zo28ak/discussion). Por lo tanto, de forma predeterminada, cada widget siguiente en 'LinearLayout' está alineado con la línea base de su widget anterior. En nuestro caso ** la primera ** línea de 'TextView' está alineada con la línea base del texto del' Botón'.Estoy de acuerdo, no es un comportamiento intuitivo, pero lo es. – StenaviN

0

Intente configurar la layout_gravity para la Vista de Texto así:

<TextView 
    android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this." 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#533f93" 
    android:layout_gravity="top" 
/> 
+0

No funcionará – StenaviN

2

dan la layout_gravity del Textview ser center_vertical

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:background="#800080"> 

<Button 
    android:text="This" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
/> 
<Button 
    android:text="That" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
/> 
<TextView 
    android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this." 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#533f93" 
android:layout_gravity="center_vertical"/> 
</LinearLayout> 
+0

Gah! Tan fácil, gracias! El valor predeterminado parece ser NINGUNO. ¿Alguna idea de por qué, por defecto, empuja el texto parcialmente fuera de la vista? – TomDestry

0

Si usted tiene este problema con varios TextViews puede agregar android:gravity="center_vertical" a LinearLayout

Cuestiones relacionadas