2012-05-23 234 views
7

我試圖讓TextView有兩段文字,一段對着最左側和一對最左側對齊。我可以在這兩個詞之間留出空格,但我想知道是否有更好的技術?Android TextView將文本對齊到右側和左側

<?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> 

回答

16

這裏的另一種方法:

<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告訴消減nt把孩子放在哪裏。在FrameLayout中,孩子們可以去任何地方,並且不會被其他孩子阻擋。如果有重疊,則添加的最後一個孩子在最上面。祝你好運!

+0

但是,當你想使用後退按鈕清除輸入的文本,因爲當過你專注編輯文本將其設置光標在輸入文本的乞討它不能正常工作。 –

4

爲什麼不來代替干擾文成單一的TextView

<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> 

您可以設置機器人2個textviews:重力=右側文本框中輸入「正確的」,如果你想要的文字是右對齊

1

這肯定會爲你工作...

<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> 
+3

op要求提供LinearLayout。另外,「請投票」太可愛了...... – katzenhut

1

這可能是更好,因爲它不允許文本重疊。

<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> 
+0

這不會將文本對齊到右邊緣 –

1

如果你需要的是一個單一的線(標籤:...值,或者類似的東西),你其實可以做到這一點使用Alignment跨度。唯一的問題是,跨度適用於整個段落,所以你必須將你的左/右文本放入單獨的段落中,並使用「零線高度」跨度將它們合併到一行中。

事情是這樣的:

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); 
} 

LineOverlapSpan是這樣實現的:

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; 
    } 
} 

詳情請參閱this answer

0

這是我找到的最簡單的方法。訣竅是在第二個文本視圖上使用match_parent,以便佔用剩餘空間並允許gravity屬性將文本移至末尾。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Left Aligned" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="end" 
     android:text="Right Aligned" /> 
</LinearLayout> 
相關問題