2011-02-10 110 views
5

我試圖讓文本行,這將是這樣的Android的佈局使用layout_weight,layout_width和maxWidth

foofoofoo - barbarbar

,但我希望它橢圓foo和bar,如果它贏得了」 t適合一條線。 即我試圖縮短它們。

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView android:id="@+id/text_1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:maxWidth="0dip" 
     android:layout_weight="1" 
     android:textColor="#ffffff" 
     android:singleLine="true" 
     android:ellipsize="true" 
     android:text="foofoofoofoo" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#ffffff" 
     android:text=" - " /> 
    <TextView 
     android:id="@+id/text_2" 
     android:layout_width="wrap_content"       
     android:layout_height="wrap_content" 
     android:maxWidth="0dip" 
     android:layout_weight="1" 
     android:textColor="#ffffff" 
     android:singleLine="true" 
     android:ellipsize="true" 
     android:text="barbarbarbar" /> 
</LinearLayout> 

熊與我這部分工作。

設置TextViewlayout_width0diplayout_weight1意味着它們會佔用每個可用空間的50%。

設置singleLinetrueellipsizetrue意味着他們會像foofoo ...如果文本比容器大。

因此,我的結果(如果文字是更長的時間)應該是

foofoo .. - BARBAR ..

它是!所以這個工作。

現在我試圖解決的情況下,如果第一TextView(ID:文本1)具有文本小於由layout_width="0dip"layout_weight="1"我希望它wrap_content給出的50%。否則,它看起來像:

空白- BARBAR ..

,我想

富 - barbarbar

這就是爲什麼我更改了layout_width="wrap_content"並添加了android:maxWidth="0dip"但這不起作用!它似乎無視我說wrap_content,仍然給這個觀點50%!

我讀到,你需要添加android:adjustViewBounds="true"maxWidth工作,但這沒有任何影響。

回答

10

這是我能做的最好的,嘗試更改字符串以查看它是否按預期工作。

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:shrinkColumns="0,2" 
    android:stretchColumns="0,2"> 
    <TableRow> 
     <TextView android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:ellipsize="end" 
      android:text="foofoofoo"/> 
     <TextView android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="-" 
      android:gravity="center_horizontal"/> 
     <TextView android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:ellipsize="end" 
      android:text="barbarbarbarbarbarbarbarbarbarbarbar"/> 
    </TableRow> 
</TableLayout> 
2

很難確切地告訴你想要什麼,但我相信答案是你不能這樣做,除非你動態調整你的代碼佈局。原因是因爲你要求系統以多種不同的方式工作。

首先,當視圖中的文本數量相同時,您希望視圖平均分享空間。

其次,您希望的視圖不是當另一視圖中的文本較少時,可以平等地分享空間。

這裏有多種其他情況可以任何你想要的方式處理。然而,我想傳達的是,你要求相同的佈局以不同的方式處理事情,這就是爲什麼你無法通過單一佈局實現這一點。

+0

我想基本上說maxWidth是共享空間(因此elipsing),但通常只是wrap_content。所以我會假設它會wrap_content,除非wrap_content的dip值> 50%,那麼它會使用maxWidth(這是50%),不是? – Blundell 2011-02-10 20:25:29

+0

`除非wrap_content的dip值> 50%,那麼它會使用maxWidth` ...我真的不知道你想說什麼。 `wrap_content`不使用dip值。 – user432209 2011-02-10 23:38:38

相關問題