2015-05-24 2 views
0

Я использую GridView, чтобы добавить 9 ImageButtons, которые работают как меню на экране. ImageButtons имеют фиксированные размеры. Проблема в том, что эти кнопки не растягиваются, чтобы заполнить их родителя GridView. Я хочу, чтобы они растянулись, чтобы заполнить оставшееся пространство.Дети GridView должны растягиваться, чтобы заполнить их родителя GridView.

Есть ли у меня имущество?

Есть ли способ реализовать это с адаптера?

Или я должен принять трудный способ расширения GridView самостоятельно?

ответ

0

Я не смог найти простое решение моей проблемы с использованием GridView поэтому я использовал GridLayout и с помощью принятый ответ:

GridLayout (not GridView) how to stretch all children evenly

Я изменил мою раскладку, чтобы иметь 9 кнопок, которые протягивают надлежащим образом я отправляю макет для справок:

<GridLayout 
    android:id="@+id/hotelHomeGridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/footerContainer" 
    android:layout_below="@id/hotel_logo" 
    android:columnCount="2" 
    android:columnWidth="90dp" 
    android:horizontalSpacing="10dp" 
    android:numColumns="auto_fit" 
    android:stretchMode="spacingWidthUniform" 
    android:verticalSpacing="10dp" > 

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

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button 1" /> 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start" 
      android:text="Button 2" /> 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start" 
      android:text="Button 3" /> 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_gravity="left|center_vertical" 
     android:layout_row="0" 
     android:orientation="horizontal" > 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button 4" /> 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button 5" /> 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start" 
      android:text="Button 6" /> 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_gravity="left|bottom" 
     android:layout_row="0" 
     android:orientation="horizontal" > 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button 7" /> 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button 8" /> 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start" 
      android:text="Button 9" /> 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</GridLayout> 
0

Похоже, вам нужно установить свойство stretchMode вашего GridView. Если я правильно понял, вы хотите, чтобы ImageButtons оставались одного размера, но распространялись через GridView. Для этого вам нужно будет что-то вроде:

<GridView 
    android:id="@+id/grid_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:numColumns="3" 
    android:columnWidth="80dp" 
    android:stretchMode="spacingWidthUniform" 
    > 

См: Android Developer - Grid View - stretchMode для различных вариантов

+0

Да, это то, о чем я так думал. Но проверьте это http://imgur.com/GhQhH1y. Я все равно получаю ту же проблему –

+0

А, вы хотите, чтобы они заполнили вертикальное пространство, а также горизонтальное? – Jahnold

+0

Да. Горизонтально я хороший. Вертикаль - моя проблема. –

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