2015-06-17 7 views
0

У меня есть 3 столбца кнопок, но в столбцах не все одинаковое количество кнопок. Какова наилучшая практика для того, чтобы все кнопки отображались одинакового размера? Я не хочу явно указывать ширину кнопок, и я сомневаюсь, что лучше всего добавить кнопки «фиктивные».Android: Как установить ширину столбца для неравного количества кнопок?

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:stretchColumns="*" 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
     <Button 
      android:layout_width="0dip" 
      android:layout_weight="1" 
      android:text="Button 1" 
      android:id="@+id/button1" /> 
     <Button 
      android:layout_width="0dip" 
      android:text="Button 2" 
      android:id="@+id/button2" 
      android:layout_weight="1" /> 
     <Button 
      android:layout_width="0dip" 
      android:text="Button 3" 
      android:id="@+id/button3" 
      android:layout_weight="1"/> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
     <Button 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:text="Button 4" 
      android:id="@+id/button4" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:text="Button 5" 
      android:id="@+id/button5" 
      android:layout_column="3" /> 
    </TableRow> 

</TableLayout> 

ответ

1

Это больше похоже на проблемы математики. Для центрирования коэффициентов (3) и даже (2) в расширяемом пространстве сетки вам понадобится всего шесть ячеек.

Вот решение (без каких-либо дополнительных представлений и не используя вес макета):

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:stretchColumns="*"> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="match_parent" 
      android:layout_column="0" 
      android:layout_span="2" 
      android:text="Button 1" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="match_parent" 
      android:layout_column="2" 
      android:layout_span="2" 
      android:text="Button 2" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="match_parent" 
      android:layout_column="4" 
      android:layout_span="2" 
      android:text="Button 3" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="match_parent" 
      android:id="@+id/button4" 
      android:layout_column="1" 
      android:layout_span="2" 
      android:text="Button 4" /> 

     <Button 
      android:id="@+id/button5" 
      android:layout_width="match_parent" 
      android:layout_column="3" 
      android:layout_span="2" 
      android:text="Button 5" /> 
    </TableRow> 
</TableLayout> 
0

Вы можете добавить три кнопки, как обычно, и скрыть последнюю кнопку, как показано ниже

<TableLayout 
 
    xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:stretchColumns="*" 
 

 
    <TableRow 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" > 
 
     <Button 
 
      android:layout_width="0dip" 
 
      android:layout_weight="1" 
 
      android:text="Button 1" 
 
      android:id="@+id/button1" /> 
 
     <Button 
 
      android:layout_width="0dip" 
 
      android:text="Button 2" 
 
      android:id="@+id/button2" 
 
      android:layout_weight="1" /> 
 
     <Button 
 
      android:layout_width="0dip" 
 
      android:text="Button 3" 
 
      android:id="@+id/button3" 
 
      android:layout_weight="1"/> 
 
    </TableRow> 
 

 
    <TableRow 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" > 
 
     <Button 
 
      android:layout_width="0dip" 
 
      android:layout_weight="1" 
 
      android:text="Button 4" 
 
      android:id="@+id/button4" /> 
 
     <Button 
 
      android:layout_width="0dip" 
 
      android:text="Button 5" 
 
      android:id="@+id/button5" 
 
      android:layout_weight="1" /> 
 
     <Button 
 
      android:layout_width="0dip" 
 
      android:text="Button 6" 
 
      android:id="@+id/button6" 
 
      android:layout_weight="1" 
 
      android:visibility="invisible"/> 
 
    </TableRow> 
 

 
</TableLayout>

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