2015-05-24 24 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

聽起來你需要設置GridView的stretchMode屬性。如果我理解正確,您希望ImageButton保持相同的大小,但可以跨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

是的。水平我很好。垂直是我的問題。 –

相關問題