2010-09-24 25 views
61

我想要兩個TextView元素並排顯示(在列表項中),一個對齊左側,一個向右。喜歡的東西:兩個TextViews並排,只有一個橢圓?

|<TextView>    <TextView>| 

(該|表示屏幕的四肢)

然而,在左邊的TextView可以有內容,太長,不適合在屏幕上。在這種情況下,我想要它的橢圓形,但仍顯示整個權利TextView。喜歡的東西:

|This is a lot of conte...<TextView>| 

我曾在這無數次嘗試,同時使用LinearLayoutRelativeLayout,我想出了唯一的解決辦法是使用RelativeLayout,把左邊TextView大到足以清除marginRight正確的TextView。然而,正如你可以想象的那樣,這不是最佳的。

還有其他解決方案嗎?

最後,LinearLayout解決方案:

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
    > 
    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_weight="1" 
     android:ellipsize="end" 
     android:inputType="text" 
     /> 
    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_weight="0" 
     android:layout_gravity="right" 
     android:inputType="text" 
     /> 
</LinearLayout> 

老,TableLayout解決方案:

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1" 
    android:shrinkColumns="0" 
    > 
    <TableRow> 
     <TextView android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="end" 
      android:singleLine="true" 
      /> 
     <TextView android:id="@+id/date" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:singleLine="true" 
      android:ellipsize="none" 
      android:gravity="right" 
      /> 
    </TableRow> 
</TableLayout> 

回答

14

使用TableLayout,放在錶行兩個TextView的,有一個嘗試。我還沒有試過

+1

謝謝,這最終奏效。我編輯了問題以包含我找到的解決方案。 – Felix 2010-09-24 08:50:08

+0

:) :)ü歡迎... – 2010-09-24 10:33:56

0

爲什麼不在右邊的TextView中放置左邊距?我正在使用這種方法的一個

|<TextView>  <ImageButton>| 

它的工作原理。

+0

它可能適合你,因爲你的ImageButton總是有相同的寬度。礦區不同,所以它不適合我。 – Felix 2010-09-24 08:45:14

+0

很高興TableLayout爲你工作:)這是一個類似的問題與自我調整按鈕http://stackoverflow.com/questions/3530182/linearlayout-text-wrap-problem-when-having-two-buttons-of-same -size-plus-one-stre/3530322 – Maragues 2010-09-24 09:03:16

17

只是一個想法,爲什麼不先在xml佈局中聲明右邊的textview並將其寬度設置爲包裝內容android:layout_alignParentRight="true"android:gravity="right"。然後在左邊聲明文本視圖,將其寬度設置爲填充父項android:layout__toLeftOf = {文本視圖在右邊的ID},其中RelativeView爲根視圖。

通過首先聲明正確的textview,將首先計算其所需的寬度並佔據視圖,而左側的textview將佔用視圖的剩餘空間。

我還沒有嘗試過,雖然它可能會給你一些想法。

[更新]

我試圖創建一個XML資源佈局......它在某種程度上作品...

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView 
    android:id="@+id/right" 
    android:layout_alignParentRight="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="right" 
    android:text="right" 
    > 
    </TextView> 
    <TextView 
    android:id="@+id/left" 
    android:layout_alignParentLeft="true" 
    android:layout_toLeftOf="@id/right" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:ellipsize="end" 
    android:lines="1" 
    android:singleLine="true" 
    android:maxLines="1" 
    android:text="too looooooooooong ofskgjo sdogj sdkogjdfgds dskjgdsko jgleft" 
    > 
    </TextView> 
</RelativeLayout> 
+1

剛剛嘗試過,不起作用。原因在於'RelativeLayout'允許它的孩子重疊,所以最終左邊的'TextView'出現在'TextView'的右下方。 – Felix 2010-09-24 10:44:01

+0

我添加了一個示例xml佈局。希望這是你正在尋找的。乾杯! – capecrawler 2010-09-26 02:07:23

+0

關於出現在右側「TextView」下方的左側「TextView」,如果明確指定了佈局,則不會發生它應該位於右側「TextView」左側的情況。屬性'android:layout_toLeftOf =「@ id/right」'將會達到目的。希望這可以幫助。 – capecrawler 2010-09-26 02:13:29

14

的LinearLayout中答案爲我工作的這個相同的問題。作爲單獨的答案發布,因爲不清楚提問者做了什麼和沒有做什麼工作。

一個區別。TableLayout對我來說不太理想,因爲我有兩行數據,而且我希望底行按照這個問題描述的行爲行事,並且頂行橫跨該區域。該問題已在另一個SO問題中得到解答:Colspan in TableLayout,但LinearLayout更簡單。

雖然越來越寬的權利花了我一下。我包括在性能的縮放項目上使用0dp寬度的android lint調整。

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
    > 
    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:ellipsize="end" 
     android:inputType="text" 
     /> 
    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_weight="0" 
     android:layout_gravity="right" 
     android:inputType="text" 
     /> 
</LinearLayout> 
+2

這是最好的答案,因爲從性能的角度來看,使用LinearLayout比使用Table(或Relative)更好。 – krossovochkin 2015-09-04 10:31:28

1

有很多這樣的答案和實際上等效的重複問題。建議的方法通常有效,有點。將其放入LinearLayout,將整個包裹在一個額外的RelativeLayout中,使用TableLayout;所有這些似乎解決了它的一個更簡單的佈局,但如果你需要更復雜的內部這兩個TextView或相同的佈局將被重新使用,例如通過RecyclerView,事情很快就會被打破。

我發現的唯一解決方案一直在,不管你把它放在什麼大的佈局上,都是自定義佈局。它的實現非常簡單,並且儘可能精簡,它將保持佈局合理平坦,很容易維護 - 所以從長遠來看,我認爲這是解決問題的最佳方案。

public class TwoTextLayout extends ViewGroup { 

    public TwoTextLayout(Context context) { 
    super(context); 
    } 

    public TwoTextLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    public TwoTextLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    final int count = getChildCount(); 
    if (count != 2) 
     throw new IllegalStateException("TwoTextLayout needs exactly two children"); 

    int childLeft = this.getPaddingLeft(); 
    int childTop = this.getPaddingTop(); 
    int childRight = this.getMeasuredWidth() - this.getPaddingRight(); 
    int childBottom = this.getMeasuredHeight() - this.getPaddingBottom(); 
    int childWidth = childRight - childLeft; 
    int childHeight = childBottom - childTop; 

    View text1View = getChildAt(0); 
    text1View.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST)); 
    int text1Width = text1View.getMeasuredWidth(); 
    int text1Height = text1View.getMeasuredHeight(); 

    View text2View = getChildAt(1); 
    text2View.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST)); 
    int text2Width = text2View.getMeasuredWidth(); 
    int text2Height = text2View.getMeasuredHeight(); 

    if (text1Width + text2Width > childRight) 
     text1Width = childRight - text2Width; 

    text1View.layout(childLeft, childTop, childLeft + text1Width, childTop + text1Height); 
    text2View.layout(childLeft + text1Width, childTop, childLeft + text1Width + text2Width, childTop + text2Height); 
    } 
} 

實現簡單得不能再簡單,它只是測量兩個文本(或任何其他孩子的意見,實際上),如果他們的總寬度超過了佈局寬度,減少了第一視圖的寬度。

如果您需要修改,例如。對齊第二個文本到第一的基線,就可以解決很容易,太:

text2View.layout(childLeft + text1Width, childTop + text1Height - text2Height, childLeft + text1Width + text2Width, childTop + text1Height); 

或任何其他解決方案,比如萎縮第二視圖相對於第一,對準的權利,等等。