GridView 子级应该伸展以填充他们的 GridView 父级
GridView children should stretch out to fill their GridView parent
我使用 GridView
来添加 9 个 ImageButtons
作为屏幕中的菜单。 ImageButtons
具有固定尺寸。问题是这些按钮没有展开以填充它们的 GridView
父按钮。我想让它们伸展开来填满剩余的 space.
有属性我遗漏的吗?
有什么方法可以从适配器实现它吗?
还是我应该采取艰难的方式自行扩展 GridView
?
听起来您需要设置 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 了解不同的选项
我无法使用 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>
我使用 GridView
来添加 9 个 ImageButtons
作为屏幕中的菜单。 ImageButtons
具有固定尺寸。问题是这些按钮没有展开以填充它们的 GridView
父按钮。我想让它们伸展开来填满剩余的 space.
有属性我遗漏的吗?
有什么方法可以从适配器实现它吗?
还是我应该采取艰难的方式自行扩展 GridView
?
听起来您需要设置 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 了解不同的选项
我无法使用 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>