2011-09-15 2 views
0

Ниже приведено определение макета. В основном, я хочу, чтобы две кнопки были одинаковой ширины, но сейчас их ширина определяется строкой, которую они отображают, есть ли лучший способ выложить это так, чтобы кнопки были одинаковой ширины и что комбинация кнопок + 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> 

Edit: Я попытался с помощью 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

хитрость заключается в том, чтобы объединить TableView, определенный в редактировании выше с 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 вместо вложенного LinearLayout вещи, которую вы в настоящее время делаете
  2. Изменения вложенности от вертикального родителя с горизонтальными детьми до горизонтального родителя с вертикальными детьми (однако это открывает возможные проблемы вертикального позиционирования, так что я не жалую эту опцию)
  3. Используйте RelativeLayout и все layout_ * атрибуты, которые RelativeLayout дети доступны для них

Я предпочитаю вариант 1, лично.

+0

спасибо за совет, но как я могу получить EditText, чтобы заполнить оставшуюся часть экрана – slayton

0

вы можете использовать определенную ширину и высоту для laout_width/layout_height, как в layout_width = "150dip". Вы рискуете иметь кнопку, которая не может соответствовать всем вашим текстам, но если ваш текст исправлен, вы можете дважды проверить, чтобы убедиться, что все в порядке.

Смежные вопросы