Android TextView Alinear texto a derecha e izquierda

Estoy tratando de tener un TextView que tiene dos partes de texto, uno alineado contra el extremo izquierdo y uno contra el extremo izquierdo. Puedo poner espacios entre las dos palabras, pero me preguntaba si hay una mejor técnica?

<?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" android:background="@drawable/background_common" android:id="@+id/LinearLayout0123"> <TextView android:layout_width="300dp" android:layout_height="40dp" android:textColor="@color/Black" android:textStyle="bold" android:textSize="15dp" android:paddingLeft="10dp" android:paddingTop="10dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:background="@drawable/selector_social_facebook_twitter" android:text="Right Text Left Text" /> </LinearLayout> 

Aquí hay otro método:

 <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background_common"> <TextView android:text="Left Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left"/> <TextView android:text="Right Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right"/> </FrameLayout> 

Layout_gravity le dice al padre dónde poner al niño. En un FrameLayout, los niños pueden ir a cualquier lugar y no están obstruidos por otros niños. Si hay solapamiento, el último hijo agregado está en la parte superior. ¡Buena suerte!

¿Por qué no tener dos vistas de texto en lugar de atascar el texto en un solo texto?

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="300dip" android:layout_height="fill_parent" android:background="@drawable/background_common" android:id="@+id/LinearLayout0123"> <TextView android:layout_weight="1" android:layout_width="0dip" android:layout_height="40dp" android:textColor="@color/Black" android:textStyle="bold" android:textSize="15dp" android:paddingLeft="10dp" android:paddingTop="10dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:background="@drawable/selector_social_facebook_twitter" android:text="Left Text" /> <TextView android:layout_weight="1" android:layout_width="0dip" android:layout_height="40dp" android:textColor="@color/Black" android:textStyle="bold" android:textSize="15dp" android:paddingLeft="10dp" android:paddingTop="10dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:background="@drawable/selector_social_facebook_twitter" android:text="Right Text" /> </LinearLayout> 

Puede configurar android: gravity = "right" en el cuadro de texto derecho si desea que el texto se alinee a la derecha

Esto podría ser mejor porque no permite que los textos se superpongan.

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="start" /> <TextView android:id="@+id/sub_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1 android:layout_gravity="end" /> </LinearLayout> 

Esto definitivamente funcionará para usted …

 <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:text="Left Side" android:textSize="15sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:text="Right Side" android:textSize="15sp" android:textStyle="bold" /> </RelativeLayout> 

Si todo lo que necesita es una sola línea (etiqueta: … valor, o algo así), en realidad puede lograr esto utilizando el intervalo de alineación . El único problema es que el span funciona para todo el párrafo, por lo que tienes que poner tus textos izquierdo / derecho en párrafos separados y fusionarlos utilizando una "altura cero-altura" en una sola línea.

Algo como esto:

 public void setLeftRightText(TextView view, String left, String right) { SpannableString merged = new SpannableString(left + "\n" + right); merged.setSpan( new AlignmentSpan.Standard(Layout.Alignment.ALIGN_NORMAL), 0, left.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE ); merged.setSpan( new LineOverlapSpan(), left.length(), left.length() + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE ); merged.setSpan( new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE), left.length() + 1, left.length() + 1 + right.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE ); view.setText(merged); } 

Donde el LineOverlapSpan se implementa de la siguiente manera:

 public class LineOverlapSpan implements LineHeightSpan { @Override public void chooseHeight(final CharSequence text, final int start, final int end, final int spanstartv, final int v, final Paint.FontMetricsInt fm) { fm.bottom += fm.top; fm.descent += fm.top; } } 

Vea esta respuesta para más detalles.

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.