android - 如何使 GridLayout 适合屏幕大小

标签 android widget

GridLayout API 文档很难学......
有没有人可以教我如何设置子Views有类似 LinearLayout 的“权重”?

现在好像全部都放在左边了,
我试了很多次还是不能让每个都像屏幕的一半宽度。

已编辑:当 child 都是 wrap_content 时,我不知道该怎么办...... 即使我想设置一些特定尺寸的图像,这个类也可以帮助我制作 ImageView wrap_content .........它不能正常运行,我是不是错过了一些设置?!?

最佳答案

注意:随着 Android 'Lollipop' 5 的引入,水平线下方的信息不再准确,因为 GridLayout 确实 适应从 API 级别 21 开始的权重原理。

引自 Javadoc :

Excess Space Distribution

As of API 21, GridLayout's distribution of excess space accomodates the principle of weight. In the event that no weights are specified, the previous conventions are respected and columns and rows are taken as flexible if their views specify some form of alignment within their groups. The flexibility of a view is therefore influenced by its alignment which is, in turn, typically defined by setting the gravity property of the child's layout parameters. If either a weight or alignment were defined along a given axis then the component is taken as flexible in that direction. If no weight or alignment was set, the component is instead assumed to be inflexible.

Multiple components in the same row or column group are considered to act in parallel. Such a group is flexible only if all of the components within it are flexible. Row and column groups that sit either side of a common boundary are instead considered to act in series. The composite group made of these two elements is flexible if one of its elements is flexible.

To make a column stretch, make sure all of the components inside it define a weight or a gravity. To prevent a column from stretching, ensure that one of the components in the column does not define a weight or a gravity.

When the principle of flexibility does not provide complete disambiguation, GridLayout's algorithms favour rows and columns that are closer to its right and bottom edges. To be more precise, GridLayout treats each of its layout parameters as a constraint in the a set of variables that define the grid-lines along a given axis. During layout, GridLayout solves the constraints so as to return the unique solution to those constraints for which all variables are less-than-or-equal-to the corresponding value in any other valid solution.

另外值得注意的是android.support.v7.widget.GridLayout包含相同的信息。不幸的是,它没有提到它引入了哪个版本的支持库,但 commit that adds the functionality可以追溯到 2014 年 7 月。2014 年 11 月,improvements in weight calculation and a bug was fixed .

为安全起见,请务必导入最新版本的 gridlayout-v7 库。


GridLayout 不存在您所描述的“权重”原则。 documentation 中明确提到了此限制。 ;摘录如下。话虽如此,有一些可能性可以使用“重力”来分配多余的空间。我建议您通读链接的文档。

Limitations

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. Some common use-cases may nevertheless be accommodated as follows. To place equal amounts of space around a component in a cell group; use CENTER alignment (or gravity). 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. When using either of these techniques, bear in mind that cell groups may be defined to overlap.

有关示例和一些实用指南,请查看 last year's blog post introducing the GridLayout widget .


编辑:我认为没有一种基于 xml 的方法可以像在 Google Play 应用程序中那样将图 block 缩放为这些正方形长度的两倍的“正方形”或“矩形”。但是,如果您以编程方式构建布局,这当然是可能的。您真正需要知道的只是设备的屏幕尺寸。

低于(非常!)Google Play 应用中平铺布局的快速近似值。

Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;
int halfScreenWidth = (int)(screenWidth *0.5);
int quarterScreenWidth = (int)(halfScreenWidth * 0.5);

Spec row1 = GridLayout.spec(0, 2);
Spec row2 = GridLayout.spec(2);
Spec row3 = GridLayout.spec(3);
Spec row4 = GridLayout.spec(4, 2);

Spec col0 = GridLayout.spec(0);
Spec col1 = GridLayout.spec(1); 
Spec colspan2 = GridLayout.spec(0, 2);

GridLayout gridLayout = new GridLayout(this);
gridLayout.setColumnCount(2);
gridLayout.setRowCount(15);

TextView twoByTwo1 = new TextView(this);
GridLayout.LayoutParams first = new GridLayout.LayoutParams(row1, colspan2);
first.width = screenWidth;
first.height = quarterScreenWidth * 2;
twoByTwo1.setLayoutParams(first);
twoByTwo1.setGravity(Gravity.CENTER);
twoByTwo1.setBackgroundColor(Color.RED);
twoByTwo1.setText("TOP");
twoByTwo1.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByTwo1, first);

TextView twoByOne1 = new TextView(this);
GridLayout.LayoutParams second = new GridLayout.LayoutParams(row2, col0);
second.width = halfScreenWidth;
second.height = quarterScreenWidth;
twoByOne1.setLayoutParams(second);
twoByOne1.setBackgroundColor(Color.BLUE);
twoByOne1.setText("Staff Choices");
twoByOne1.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByOne1, second);

TextView twoByOne2 = new TextView(this);
GridLayout.LayoutParams third = new GridLayout.LayoutParams(row2, col1);
third.width = halfScreenWidth;
third.height = quarterScreenWidth;
twoByOne2.setLayoutParams(third);
twoByOne2.setBackgroundColor(Color.GREEN);
twoByOne2.setText("Games");
twoByOne2.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByOne2, third);

TextView twoByOne3 = new TextView(this);
GridLayout.LayoutParams fourth = new GridLayout.LayoutParams(row3, col0);
fourth.width = halfScreenWidth;
fourth.height = quarterScreenWidth;
twoByOne3.setLayoutParams(fourth);
twoByOne3.setBackgroundColor(Color.YELLOW);
twoByOne3.setText("Editor's Choices");
twoByOne3.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse);
gridLayout.addView(twoByOne3, fourth);

TextView twoByOne4 = new TextView(this);
GridLayout.LayoutParams fifth = new GridLayout.LayoutParams(row3, col1);
fifth.width = halfScreenWidth;
fifth.height = quarterScreenWidth;
twoByOne4.setLayoutParams(fifth);
twoByOne4.setBackgroundColor(Color.MAGENTA);
twoByOne4.setText("Something Else");
twoByOne4.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByOne4, fifth);

TextView twoByTwo2 = new TextView(this);
GridLayout.LayoutParams sixth = new GridLayout.LayoutParams(row4, colspan2);
sixth.width = screenWidth;
sixth.height = quarterScreenWidth * 2;
twoByTwo2.setLayoutParams(sixth);
twoByTwo2.setGravity(Gravity.CENTER);
twoByTwo2.setBackgroundColor(Color.WHITE);
twoByTwo2.setText("BOTOM");
twoByTwo2.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse);
gridLayout.addView(twoByTwo2, sixth);

结果看起来有点像这样(在我的 Galaxy Nexus 上):

Tiled layout using GridLayout

关于android - 如何使 GridLayout 适合屏幕大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10347846/

相关文章:

java - Toast 不会在 Android 应用程序上显示,使用 android studio

java - 具有不同行号的Android GridView

android - 在Android中,Matrix类的mapRect api执行什么样的转换?

具有多个按钮的android小部件

python-3.x - Jupyter 笔记本按钮小部件 : Remove all click handlers?

android - 是否可以在 Flutter 中使用手写笔输入?

Android - 单击 ListView 项目时固定 map 位置?

go - 导航并添加到TabItem中的内容

android - 设置 switchStyle - 找不到错误资源 - 为什么?

Android gcm 项目服务器 key 需要一次又一次重新生成