Tratar de presionar un botón hasta el fondo del diseño lineal en Android

Sé que esto se puede hacer muy fácilmente usando un Layout Relativo, pero siento que lo que estoy haciendo es correcto y debería darme el resultado deseado con el Linear Layout. Pero por alguna razón, cuando corro en esto en Google Nexus 7 con Android JB 4.1.2, veo el botón y la vista de texto inmediatamente después de los elementos de vista de lista. Si la vista de lista está vacía, los veo en la parte superior de la pantalla. ¿¿Estoy haciendo algo mal?? Aquí está mi archivo xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/attachments_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1" android:orientation="vertical"> <ListView android:id="@+id/mtg_attachments" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> <Button android:id="@+id/attach_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/delete" android:textSize="22sp" /> <TextView android:text="@string/attach_warning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout> 

Si desea centrar un elemento en el centro de la pantalla, no utilice LinearLayout ya que están destinados a mostrar una serie de elementos en una fila apilados uno tras otro.

Utilice un RelativeLayout en su lugar.

Así que reemplazar:

 android:layout_gravity="center" 

Para la correspondiente opción RelativeLayout :

 android:layout_centerInParent="true" 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/attachments_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="1" > <ListView android:id="@+id/mtg_attachments" android:layout_width="fill_parent" android:layout_height="70dp" android:layout_weight="0.9" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="0.1" android:gravity="bottom" > <Button android:id="@+id/attach_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="delete" android:textSize="22sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="attach_warning" android:textSize="18sp" /> </LinearLayout> </LinearLayout> 

Sriman por favor eche un vistazo a esta respuesta y luego dígame si esto es útil.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/attachments_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="bottom" android:orientation="vertical" android:weightSum="1" > <ListView android:id="@+id/mtg_attachments" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:id="@+id/attach_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="delete" android:textSize="22sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="attach_warning" android:textSize="18sp" /> </LinearLayout> </LinearLayout> 

Modificar su código xml.

Usando el android:layout_weight para ajustar el porcentaje de su diseño.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/attachments_list" android:layout_width="fill_parent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1" android:orientation="vertical"> <ListView android:id="@+id/mtg_attachments" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="0.8" /> <Button android:id="@+id/attach_delete" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center" android:layout_weight="0.1" android:text="@string/delete" android:textSize="22sp" /> <TextView android:text="@string/attach_warning" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="0.1" android:textSize="18sp" /> </LinearLayout> 

Sé que esta pregunta ya tiene una respuesta aceptada, pero voy a responder de todos modos ya que no se encontró ninguna solución y este es el primer resultado que google me dio acerca de este problema que también tenía.

Simplemente coloque un LinearLayout (vertical) con el layout:weight = 1 entre los 2 elementos que desea dividir.

En su código, eso sería

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/attachments_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1" android:orientation="vertical"> <ListView android:id="@+id/mtg_attachments" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="0" /> <LinearLayout <!-- You should add this one and remove android:layout_weigth="1" form the listview --> android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> </LinearLayout> <Button android:id="@+id/attach_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/delete" android:textSize="22sp" /> <TextView android:text="@string/attach_warning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout> 

Tenga en cuenta que el LinearLayout añadido tiene que ser el único con el conjunto de propiedades de peso, de lo contrario tenemos que hacer frente a algún problema.

  • Android ScrollView fillViewport no funciona
  • Configuración de layout_width, layout_height mediante un estilo
  • ¿Cómo igualmente span botones / diseños verticalmente en android?
  • CoordinatorLayout estropea la posición emergente setError
  • Disposición de Android: ¿Cómo mantener el elemento de texto más a la derecha y elipse el elemento de texto de la izquierda a medida que crece?
  • Android TextView y el botón de lado a lado, ellipsize izquierda TextView
  • ¿Por qué se puede hacer clic en un botón cuando hay otro diseño que lo superpone?
  • Cómo establecer gravedad y gravedad de diseño de un LinearLayout programatically
  • Cómo inflar correctamente un diseño con un ViewFlipper anidado?
  • Android: Cómo establecer el tamaño de un diseño de forma programática
  • Cómo obtener programatically ancho y alto de relativo - Linear layout en android?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.