2010-08-20 51 views
2

看起來像關於居中,相同的大小等有很多問題,但到目前爲止我沒有找到確切的我的情況,所以我敢問:)LinearLayout文本包裝問題,當有兩個相同大小的按鈕加上一個在中心延伸

我需要的是三個按鈕像這樣的佈局:

[ previous ][*select*] [ next ] 

其中[上一頁][下一步]按鈕相同大小(即在此的情況下,尺寸爲[上一個]按鈕,因爲它更大),並且[* select *]按鈕應拉伸以佔據所有可用寬度。

繼LinearLayout中同樣大小製作兩個按鈕的提示,我想出了下面的XML文件:

<LinearLayout 
    android:id="@+id/button_bar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" > 

    <Button 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Previous" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:text="Select" /> 

    <Button 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Next" /> 

</LinearLayout> 

幾乎作品:) 除了一兩件事:反而讓下一頁按鈕大小匹配上一頁按鈕,android製作上一頁按鈕的大小爲下一頁 :) 因爲這個文本「上一頁」被包裹在兩行,像

Previ 
ous 

說不上來,如果這是一個錯誤或沒有,但你可以指點我解決方法或一些其他的方式才達到所需的佈局?

謝謝!

+0

啊,我也試圖爲按鈕指定textview的「android:singleLine = true」屬性 - 它沒有幫助 – dimsuz 2010-08-20 10:53:41

+0

有類似問題的帖子 http://stackoverflow.com/questions/1177020/android-how -to-化妝所有元素 - 內 - linearla yout-same-size – DeRagan 2010-08-20 11:15:44

+0

是的,我用這個帖子來製作我的xml。但事情是,雖然它有些作品,我有一個問題與包裝文本(請參閱我的問題的結束部分) – dimsuz 2010-08-20 11:33:45

回答

1

我建議使用一個RelativeLayout的

<RelativeLayout 
    android:id="@+id/buttons" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

    android:clickable="false"> 

    <Button 
    android:id="@+id/previous" 

    android:layout_width="30dp" 
    android:layout_height="wrap_content" 
    android:text="Previous" 

    android:layout_alignParentLeft="true"/> 

    <Button 
    android:id="@+id/next" 

    android:layout_width="30dp" 
    android:layout_height="wrap_content" 
    android:text="Next" 

    android:layout_alignParentRight="true"/> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Select" 

    android:layout_toRightOf="@id/previous" 
    android:layout_toLeftOf="@id/next" /> 
</RelativeLayout> 

這應該工作。

+0

試過了,但結果是不完全是我想要的:上一個和下一個按鈕是不同的寬度,而我想Next會像以前一樣:) – dimsuz 2010-08-20 11:24:55

+0

這不工作要麼大聲笑...瘋狂的佈局問題! – Sephy 2010-08-20 11:35:33

+1

您可以手動設置按鈕的寬度(答案更新),然後它們將是相同的寬度。我不知道這是否有幫助,也許你不想要一個固定的寬度。 – Maragues 2010-08-20 12:06:41

0

在那種情況下,我會去一個TableLayout,其中你有一行,每個按鈕的權重爲1。並將stretchColumn屬性放置到列1.這是否有所不同?

+0

我試過了。這個0dip技巧不適用於TableLayout。如果我沒有它,並按照你的建議去做,那麼下一個和上一個按鈕的寬度就不同了,我找不到一種方法使它們的大小相同:) – dimsuz 2010-08-20 11:09:34

0

我不確定您是否可以將尺寸與其他尺寸相匹配,除此之外,還可以在代碼中進行此操作。最簡單的解決方案可能是調整dp/sp單元中的上一個和下一個按鈕寬度,直到它們看起來不錯,並且只有選擇按鈕纔是具有layout_weight屬性的按鈕。

+0

我使用的這個0dip + layout_weight技巧是一個特殊的技巧正如我所瞭解的那樣,爲我的情況:)谷歌爲它,你會發現它。是的,我想我可以在代碼中做到這一點,但我只是尋找一種方法來在XML中做到這一點:) – dimsuz 2010-08-20 11:08:17

+0

我的意思是,如果你設置你的上一頁/下一頁按鈕說80dp的寬度和你的文本大小是在dp中定義的無論屏幕大小如何,文本應該始終適合按鈕。然後你會知道你的兩個按鈕的大小相同,並且選擇按鈕將佔用空間的其餘部分,因爲其他視圖沒有設置layout_weight。 實際上,您可能仍然想要爲所有按鈕使用layout_weight,只需將寬度設置爲允許「previous」適合一行的最小寬度即可。 – DonSteep 2010-08-20 11:12:52

+0

事情是,考慮到可能的未來翻譯到其他語言考慮到我不能依靠知道文本的最小寬度...我寧願找到一些方法來使android做所有的魔術:) – dimsuz 2010-08-20 11:19:27

1

如果你包括 「機器人:layout_weight」 你應該給任何 「機器人:layout_width」 或 「機器人:layout_height」 爲 「FILL_PARENT」

修改這樣

<LinearLayout 
android:id="@+id/button_bar" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:orientation="horizontal" > 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="2" 
    android:text="Previous" /> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.5" 
    android:text="Select" /> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="2" 
    android:text="Next" /> 

代碼
+0

不,這是行不通的。這種方式選擇按鈕甚至消失在屏幕上,因爲它試圖匹配父母的寬度兩倍於其他兩個;) – dimsuz 2010-08-20 11:29:15

+0

我已編輯帖子..這是在給重量的問題..現在它會正常工作.. 試穿吧! – Eby 2010-08-20 11:35:06

+0

你可以玩「android:layout_weight」 – Eby 2010-08-20 11:36:56

相關問題