¿Por qué android: layout_gravity = "derecho" no funciona aquí en android xml

Mi código xml para la fila de la vista de lista es

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="2" android:gravity="center" > <ImageView android:id="@+id/iv_image" android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/app_icon_17" android:contentDescription="@string/empty" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="2" android:gravity="center" > <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/btn_delete_new" android:focusable="false" /> </LinearLayout> </LinearLayout> 

Quiero que el último botón con id btn_delete se mantenga alineado a la derecha. Y su presentación como yo quería en "diseño gráfico" mientras diseñaba. Pero cuando lo ejecuto, en el emulador no está funcionando.

Ver la salida:

Introduzca aquí la descripción de la imagen

Entonces, ¿por qué el botón de borrado no está alineado a la derecha?

Use esto, usando el diseño relativo

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_name" android:layout_marginLeft="10dp" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" > <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/btn_delete_new" android:focusable="false" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_toLeftOf="@+id/rl_btn" > <ImageView android:id="@+id/iv_image" android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/app_icon_17" android:contentDescription="@string/empty" /> </RelativeLayout> </RelativeLayout> 

Parece que han interpretado mal los diseños, los LinearLayouts incrustados no son necesarios. Utilice un RelativeLayout , por ejemplo:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" /> <ImageView android:id="@+id/iv_image" android:layout_width="50dp" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/tv_time" android:background="@drawable/app_icon_17" android:contentDescription="@string/empty" /> <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:background="@drawable/btn_delete_new" android:focusable="false" /> </RelativeLayout> 

Como podemos ver que usted ha asignado peso a su disposición de la fila.

Si asignas un peso a tu 0dp padre solo necesitas ajustar la altura correspondiente o con 0dp . En tu caso android:layout_width="0dp"

Pero cuando usted tiene disposición del niño de esa disposición ponderada entonces usted debe dar esa característica de los trazados como fill_parent es decir: android:layout_height="fill_parent" del botón android:layout_height="fill_parent"

Por lo tanto, si no desea cambiar el diseño de los padres y desea trabajar con el código existente, puede probar el código a continuación.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" > <ImageView android:id="@+id/iv_image" android:layout_width="fill_parent" android:layout_height="40dp" android:background="@drawable/icon" android:contentDescription="@string/app_name" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" > <Button android:id="@+id/btn_delete" android:layout_width="fill_parent" android:layout_height="40dp" android:background="@drawable/icon" android:focusable="false" /> </LinearLayout> </LinearLayout> 

Cambia esto

 <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="2" android:gravity="center" > <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/btn_delete_new" android:focusable="false" /> </LinearLayout> 

a

 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="right" android:background="@drawable/btn_delete_new" android:focusable="false" /> </LinearLayout> 

Estabas aplicando android:layout_gravity="right" to layout. En su lugar, debe aplicarlo a un botón dentro del diseño. Y no creo que usted necesita peso 2 para ese diseño. Pero puede añadirlo si es necesario para usted. Espero eso ayude.

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:orientation="vertical" > <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="right" android:gravity="right" android:background="@drawable/btn_delete_new" android:focusable="false" /> </LinearLayout> 
  • Cambiar posición de fondo / gravedad en ImageButton
  • Configuración de la gravedad de ImageView en el centro de android mediante programación
  • ¿Cuál es la diferencia entre sensores de "gravedad" y "aceleración" en Android?
  • ¿Qué es TextView Gravity de forma predeterminada?
  • Cómo establecer la gravedad de textview programatically con RelativeLayout?
  • layout_gravity no funciona
  • Configuración de la gravedad del centro de la vista de los niños Cambiar la alineación del diseño de otra vista de los niños en la disposición horizontal?
  • Desplazamiento de Android TextView hacia abajo
  • android eliminar la gravedad de las lecturas de acelerómetro
  • El centro de gravedad RelayLayout no funciona
  • Alinear botones en GridLayout
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.