如何使用网格布局,以适应屏幕大小网格、如何使用、布局、屏幕

2023-09-11 12:44:16 作者:乱花飞絮

网格布局API文档是非常难学...... 没有任何人可以教我如何设置子视图有类似重的线性布局

The grid layout API docs is very difficult to learn ...... is there any one can teach me how to set child view to have something like " weight " of linear layout

现在它看起来像在左手侧的所有地方,我有尝试了很多次,仍不能使它像画面中的每一个半宽。任何一个知道?

now it look like all place in the left hand side , i have try a many times and still can not make it like each one half width of the screen . any one know ??

主编:我不知道我能做些什么的时候,他童装都是WRAP_CONTENT ......     甚至我想设置特定大小一些图像,该类帮我做的ImageView WRAP_CONTENT .........它不能正常工作,是我错过了一些设置?!?

Edited : i don't know what can i do for when his childen is all wrap_content ...... even i want to set some image in specific size , this class help me to make imageview wrap_content .........it cannot operate normal , is i have miss some setting ?!?

推荐答案

注意:低于水平线的信息不再准确,推出Android的棒棒堂5,如网​​格布局的确实的适应权重的原则,因为API级别21

Note: The information below the horizontal line is no longer accurate with the introduction of Android 'Lollipop' 5, as GridLayout does accommodate the principle of weights since API level 21.

从的Javadoc 引用:

多余的空间分布

由于API 21的,多余的空间可容纳网格布局的分布   重量的原理。倘若没有权重被指定,   在previous约定尊重和列和行采取   灵活的,如果他们的观点中指定某种形式调整自己的   组。视图的灵活性,因此影响了其   其中,反过来,典型地通过将重力限定对准   孩子的布局参数的属性。如果任一个重量或   对准分别沿着给定轴则组件被取定义   灵活的那个方向。如果没有重量或比对置位,则   组件,而不是假定不灵活。

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.

要进行列拉伸,确保所有组件里面   定义一个重量或重力。为prevent从伸展列,   确保在列的组分之一不定义   重量或重力。

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.

在灵活性原则并不能完全   消歧,网格布局的算法有利于行数和列   更接近其右侧和底部边缘。为了更precise,   GridLayout的对待它的每个布局参数作为一个约束   一组定义的网格线沿一给定轴的变量。   在布局,GridLayout的解决了限制,以便返回   独特的解决方案,以这些限制为其中所有变量都   低于或-等于到相应的值,在任何其他有效   解决方案。

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 ,增加了可以追溯到到2014年7月到2014年十一月,improvements在权重计算和错误是固定。

It's also worth noting that android.support.v7.widget.GridLayout contains the same information. Unfortunately it doesn't mention which version of the support library it was introduced with, but the commit that adds the functionality can be tracked back to July 2014. In November 2014, improvements in weight calculation and a bug was fixed.

为了安全起见,一定要导入GridLayout的-V7库的最新版本。

To be safe, make sure to import the latest version of the gridlayout-v7 library.

的权重的原则,为你描述它,不与网​​格布局存在。这种限制显然是提到在文档;摘录如下。话虽这么说,有一些可能使用'重心'的剩余空间分配。我建议您通过链接的文档阅读。

The principle of 'weights', as you're describing it, does not exist with GridLayout. This limitation is clearly mentioned in the documentation; excerpt below. That being said, there are some possibilities to use 'gravity' for excess space distribution. I suggest you have read through the linked documentation.

限制

网格布局不为的重量的原则提供支持,   在重定义。在一般情况下,它是不因此能够   配置网格布局,以在不平凡的分配剩余空间   多个行或列之间的比例。一些常见的用例   仍可如下被容纳。为了将等量   围绕在细胞团的部件空间;使用居中对齐(或   重力)。对于完全控制在一排剩余空间分配   或列;使用的LinearLayout子视图举行的组件   相关的细胞群。当使用这两种技术,紧   介意单元组可以被定义为重叠。

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今年的博客文章介绍了网​​格布局部件。

For an example and some practical pointers, take a look at last year's blog post introducing the GridLayout widget.

编辑:我不认为有一种基于XML的方式来缩放瓦片像在谷歌Play应用到'广场'或'长方形'的这些广场的长度的两倍。然而,这当然是可能的,如果您以编程方式建立自己的布局。你真正需要知道的二阶实现这个目标是设备的屏幕尺寸。

I don't think there's an xml-based approach to scaling the tiles like in the Google Play app to 'squares' or 'rectangles' twice the length of those squares. However, it is certainly possible if you build your layout programmatically. All you really need to know in order two accomplish that is the device's screen dimensions.

下面一个(非常!)快速'N脏逼近平铺布局在谷歌Play应用。

Below a (very!) quick 'n dirty approximation of the tiled layout in the Google Play app.

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的):

The result will look somewhat like this (on my Galaxy Nexus):