2011-09-15 38 views
0

佈局定義如下。基本上我希望兩個按鈕的寬度相同,但是現在它們的寬度取決於它們顯示的字符串,是否有更好的方法來展示這些按鈕的寬度,以及Button + EditText組合填充屏幕的寬度?如何將兩個按鈕佈置爲相同大小?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/editorlinearlayout" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"> 

    <LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <Button android:id="@+id/setNameBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text=" Set Item Name "/> 
     <EditText android:id="@+id/nameTxtBox" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 
    </LinearLayout> 
    <LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <Button android:id="@+id/setGroupBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Set Group Name"/> 
     <EditText android:id="@+id/groupTxtBox" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 
    </LinearLayout> 
</LinearLayout> 

編輯: 我使用TableLayout正如以下建議嘗試過,但現在的EditText的不填滿屏幕的剩餘部分。請參閱下面的圖片和佈局。

<TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

    <TableRow> 
     <Button android:id="@+id/setNameBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text=" Set Item Name "/> 
     <EditText android:id="@+id/nameTxtBox" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 
    </TableRow> 
    <TableRow> 
     <Button android:id="@+id/setGroupBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Set Group Name"/> 
     <EditText android:id="@+id/groupTxtBox" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 
    </TableRow> 
    </TableLayout> 

enter image description here

回答

3

,關鍵是要在編輯定義上面的實現代碼如下用strechColumn標籤結合:

<TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="1"> 
    <TableRow> 
    <Button android:id="@+id/setNameBtn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" Set Item Name "/> 
    <EditText android:id="@+id/nameTxtBox" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    </TableRow> 
</TableLayout> 
1

三個選項:

  1. 在TableLayout,而不是目前你正在做
  2. 從橫向孩子垂直家長的嵌套改爲垂直孩子水平父嵌套的LinearLayout東西(但將這個,這就打開了可能的垂直定位問題,所以我不贊成這個選項)
  3. 使用RelativeLayout和RelativeLayout子項可用的所有layout_ *屬性

我個人喜歡選項1。

+0

謝謝你的提示,但我怎麼能得到的EditText來填充屏幕的剩餘部分 – slayton

0

您可以使用laout_width/layout_height的特定寬度和高度,如layout_width =「150dip」中所示。儘管你有一個不適合你所有文本的按鈕的風險,但如果你的文本是固定的,你可以仔細檢查一下,以確保它是好的。

相關問題