2010-11-29 19 views
63

Tengo el siguiente diseño en su lugarDisposición Android Alinear a la derecha

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_weight="1"> 


     <WebView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/webview" android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </LinearLayout> 

    <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="13"> 
     <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> 
        <ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" /> 
      </LinearLayout> 

      <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> 
       <ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" /> 
      </LinearLayout> 

     </LinearLayout> 

     <RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" > 
       <ImageButton android:background="@null" android:id="@+id/special" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/> 
     </RelativeLayout> 




    </LinearLayout> 


</LinearLayout> 

Para el propósito de esta pregunta, yo soy sólo se preocupa por la mitad inferior de la distribución. En este momento contiene 3 botones de imagen. Los primeros 2, quiero uno al lado del otro alineados a la izquierda. El tercero, quiero alinearme al lado derecho.

Al igual que, los primeros 2 botones están donde quiero que estén, pero el tercero está permanentemente en línea hacia la izquierda. ¿Cómo lo haría alineado a la derecha?

Respuesta

124

El diseño es extremadamente ineficiente e hinchado. No necesita tantos LinearLayout s. De hecho, no necesita ningún LinearLayout en absoluto.

Utilice solo un RelativeLayout. Me gusta esto.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <ImageButton android:background="@null" 
     android:id="@+id/back" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/back" 
     android:padding="10dip" 
     android:layout_alignParentLeft="true"/> 
    <ImageButton android:background="@null" 
     android:id="@+id/forward" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/forward" 
     android:padding="10dip" 
     android:layout_toRightOf="@id/back"/> 
    <ImageButton android:background="@null" 
     android:id="@+id/special" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/barcode" 
     android:padding="10dip" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 
+0

Gracias, me salvó un montón de problemas. –

18

Puede hacer todo eso usando solo un RelativeLayout (que, por cierto, no necesita el parámetro android:orientation). Así, en lugar de tener un LinearLayout, que contiene un montón de cosas, se puede hacer algo como:

<RelativeLayout> 
    <ImageButton 
     android:layout_width="wrap_content" 
     android:id="@+id/the_first_one" 
     android:layout_alignParentLeft="true"/> 
    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_toRightOf="@+id/the_first_one"/> 
    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 

Como habrá notado, hay algunos parámetros XML que faltan. Solo estaba mostrando los parámetros básicos que debías poner. Puedes completar el resto.

3

Este es un ejemplo de un RelativeLayout:

RelativeLayout relativeLayout=(RelativeLayout)vi.findViewById(R.id.RelativeLayoutLeft); 
       RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams(); 
       params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
       relativeLayout.setLayoutParams(params); 

Con otro tipo de diseño (ejemplo LinearLayout) usted simplemente tiene que cambiar RelativeLayout para LinearLayout.

3

Si desea utilizar LinearLayout, puede hacer la alineación con layout_weight con el elemento Space.

E.g. siguiente distribución coloca textView y textView2 uno junto al otro y textView3 será alineado a la derecha

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/textView" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/textView2" /> 

    <Space 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="20dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/textView3" /> 
</LinearLayout> 

se puede lograr el mismo efecto sin Space si desea establecer layout_weight a textView2. Es solo que me gustan las cosas más separadas, además de demostrar el elemento Space.

<TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/textView2" /> 

Tenga en cuenta que you should (not must though) setlayout_width de forma explícita, ya que se volverá a calcular de acuerdo a su peso de todos modos (misma manera que debe establecer la altura de los elementos verticales de LinearLayout). Para otras sugerencias de rendimiento de diseño, consulte la serie Android Layout Tricks.

1

Para admitir versiones anteriores, el espacio se puede reemplazar con la vista como a continuación. Agregue esta vista entre el componente más a la izquierda y el componente más a la derecha. Esta vista con peso = 1 se estirará y llenará el espacio

<View 
     android:layout_width="0dp" 
     android:layout_height="20dp" 
     android:layout_weight="1" /> 

Se proporciona el código de muestra completo aquí. Tiene 4 componentes.Dos flechas estarán en el lado derecho e izquierdo. The Text and Spinner estará en el medio.

<ImageButton 
     android:id="@+id/btnGenesis" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center|center_vertical" 
     android:layout_marginBottom="2dp" 
     android:layout_marginLeft="0dp" 
     android:layout_marginTop="2dp" 
     android:background="@null" 
     android:gravity="left" 
     android:src="@drawable/prev" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="20dp" 
     android:layout_weight="1" /> 

    <TextView 
     android:id="@+id/lblVerseHeading" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:gravity="center" 
     android:textSize="25sp" /> 

    <Spinner 
     android:id="@+id/spinnerVerses" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:gravity="center" 
     android:textSize="25sp" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="20dp" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/btnExodus" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center|center_vertical" 
     android:layout_marginBottom="2dp" 
     android:layout_marginLeft="0dp" 
     android:layout_marginTop="2dp" 
     android:background="@null" 
     android:gravity="right" 
     android:src="@drawable/next" /> 
</LinearLayout> 
Cuestiones relacionadas