2012-05-17 30 views
2

假設我有一個EditText和一個Button水平相鄰。左邊我們得到了EditText,右邊是Button。Android xml:拉伸一個元素並將另一個水平包裹,彼此水平相鄰

事情是,我想要按鈕來包裝它的內容,而EditText從屏幕左側一直延伸到按鈕。所以按鈕儘可能小(取決於按鈕文本)和EditText儘可能寬(取決於按鈕的大小)

我不知道我是否錯過了一個簡單的技巧......但如何我完成了嗎?

+0

是的,你錯過了一個簡單的把戲。你使用線性佈局和權重 – keyser

+1

然後我的下一個問題是,我如何獲得權重來使按鈕的內容儘可能小? – Revolutionair

+0

沒有重量,wrap_content?那麼建議採用相對佈局的解決方案也應該可行。也許這是與權重矯枉過正 – keyser

回答

3

您必須使用RelativeLayout,使Button對齊並將EditText設置爲按鈕的左側。在那之後,EditText的fill_parent和Button的wrap_content會發揮魔力。

看下面的代碼澄清了一切。

<RelativeLayout 
    android:id="@+id/RL1" 
    ... 
    android:orientation="horizontal" > 

     <EditText 
      android:layout_width="fill_parent" 
      ... 
      android:layout_toLeftOf="@+id/buttonRight" > 
     </EditText> 

     <Button 
      android:id="@+id/buttonRight" 
      android:layout_width="wrap_content" 
      ... 
      android:singleLine="true" 
      android:layout_alignParentRight="true"> 
     </Button> 

</RelativeLayout>