GridView子项应该伸展以填充其GridView父级

时间:2015-05-24 09:53:44

标签: android gridview

我使用GridView添加9 ImageButtons作为屏幕中的菜单。 ImageButtons具有固定的尺寸。问题是这些按钮不会拉伸以填充其GridView父级。我希望它们伸展出来以填补剩余的空间。

我有遗失的财产吗?

我有没有办法从适配器实现这个?

或者我应该采取艰难的方式来扩展我自己的GridView

2 个答案:

答案 0 :(得分:0)

听起来你需要设置GridView的stretchMode属性。如果我理解正确,你希望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了解不同的选项

答案 1 :(得分: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>
相关问题