GridLayout(不是GridView)如何均匀拉伸所有子项

编程入门 行业动态 更新时间:2024-10-23 04:43:00
本文介绍了GridLayout(不是GridView)如何均匀拉伸所有子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要一个 2x2 的网格,里面有一个按钮.这只是 ICS,所以我正在尝试使用给定的新 GridLayout.

I want to have a 2x2 grid with a buttons inside. This is only ICS so I am trying to use the new GridLayout given.

这是我的布局的 XML:

Here's the XML of my layout:

<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="schemas.android/apk/res/android" android:id="@+id/favorites_grid" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:rowCount="2" android:columnCount="2"> <Button android:text="Cell 0" android:layout_row="0" android:layout_column="0" android:textSize="14dip" /> <Button android:text="Cell 1" android:layout_row="0" android:layout_column="1" android:textSize="14dip" /> <Button android:text="Cell 2" android:layout_row="1" android:layout_column="0" android:textSize="14dip" /> <Button android:text="Cell 3" android:layout_row="1" android:layout_column="1" android:textSize="14dip" /> </GridLayout>

问题是我的视图不会为每一行均匀拉伸.这会导致我的 GridLayout 右侧有很多额外的空间.

The problem is that my views do not stretch evenly for each row. This causes a lot of extra space to the right of my GridLayout.

我尝试设置 layout_gravity="fill_horizo​​ntal" 但这仅适用于行上的最后一个视图.这意味着 Cell 1 一直延伸为 Cell 0 提供足够的空间.

I tried setting layout_gravity="fill_horizontal" but that only applies to the last view on the row. This means Cell 1 stretches all the way to give enough space for Cell 0.

关于如何解决这个问题的想法?

Thoughts on how to tackle this?

推荐答案

UPDATE:从 API 21 开始支持权重.请参阅 PaulT 的回答 了解更多详情.

UPDATE: Weights are supported as of API 21. See PaulT's answer for more details.

使用GridLayout时有一些限制,以下引用来自文档.

There are limitations when using the GridLayout, the following quote is taken from the documentation.

"GridLayout 不提供对权重原则的支持,因为以重量定义.一般来说,因此不可能配置 GridLayout 以非平凡的方式分配多余的空间多行或多列之间的比例......为了完全控制行或列中过度的空间分布;使用线性布局subview 来保存相关单元组中的组件."

"GridLayout does not provide support for the principle of weight, as defined in weight. In general, it is not therefore possible to configure a GridLayout to distribute excess space in non-trivial proportions between multiple rows or columns ... For complete control over excess space distribution in a row or column; use a LinearLayout subview to hold the components in the associated cell group."

这是一个使用 LinearLayout 子视图的小例子.(我使用了占用未使用区域并将按钮推到所需位置的空间视图.)

Here is a small example that uses LinearLayout subviews. (I used Space Views that takes up unused area and pushes the buttons into desired position.)

<GridLayout xmlns:android="schemas.android/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="1" > <TextView android:text="2x2 button grid" android:textSize="32dip" android:layout_gravity="center_horizontal" /> <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" /> </LinearLayout> <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 3" /> <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 4" /> <Space android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout> </GridLayout>

更多推荐

GridLayout(不是GridView)如何均匀拉伸所有子项

本文发布于:2023-11-10 15:59:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575814.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:均匀   GridLayout   GridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!