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>
- Modo Android Landscape cortando mis botones en la parte superior
- Establecer dos botones a la misma anchura independientemente del tamaño de la pantalla?
- Adición dinámica de vistas a RelativeLayout dentro de ScrollView
- Android - LinearLayout en ScrollView en Relativelayout
- ¿Cómo crear el diseño de una vista personalizada de forma programática?
- Programe el ancho del botón de forma programada en 50% de los padres LinearLayout
- LayoutAnimation sólo funciona una vez
- Cómo colocar un LinearLayout debajo de un LinearLayout dentro de otro RelativeLayout?
- Vista personalizada que divide el diseño en diagonal con diferentes vistas secundarias
- LinearLayout marcado si se utiliza en CardView
- Cómo establecer un tema diferente en un diseño
- Iniciar dos animaciones en el mismo Layout
- Agregue dinámicamente scrollview en diseño y lineal en scrollview
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 Studio que representa la clase interna anónima
- Cómo cambiar el icono de APP dinámicamente en android