Android的网格布局API 21 [英] Android GridLayout API 21

查看:200
本文介绍了Android的网格布局API 21的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成这样的事情:

I'm trying to accomplish something like this:

目前,我手动设置的瓦片的一半屏幕宽度的宽度。这工作得很好,但它使加入的瓷砖之间的分隔(如截图所示)硬盘。幸运的是,似乎在API 21,现在有体重支持GridLayout的,引用在这里为您提供方便:

Currently, I'm manually setting the width of the tiles equal to half the screen width. This works well, but it makes adding dividers between the tiles (as seen in the screenshot) hard. Luckily, it seems that in API 21, there is now support for weight in GridLayout, quoted here for your convenience:

由于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.

我放在一起这样简单的布局,包括有2列的网格布局,试图达到两个同样宽的列:

I put together this simple layout consisting of a GridLayout with 2 columns, trying to achieve two equally-wide columns:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:columnCount="2">
        <TextView android:layout_width="wrap_content" android:layout_gravity="fill_horizontal" android:text="Hello" android:layout_columnWeight="1" />
        <TextView android:layout_width="wrap_content" android:layout_gravity="fill_horizontal" android:text="Hello" android:layout_columnWeight="1" />
        <TextView android:layout_width="wrap_content" android:layout_gravity="fill_horizontal" android:text="Hello" android:layout_columnWeight="1" />
        <TextView android:layout_width="wrap_content" android:layout_gravity="fill_horizontal" android:text="Hello" android:layout_columnWeight="1" />
        <TextView android:layout_width="wrap_content" android:layout_gravity="fill_horizontal" android:text="Hello" android:layout_columnWeight="1" />
    </GridLayout>
</RelativeLayout>

我想,既然所有的孩子都有平等的列权重,这将导致在同样范围的列。这似乎不是然而是这种情况,因为这是什么样的结果:

I thought that since all the children have equal column weight, it would result in equally-wide columns. This doesn't seem to be the case however, since this is what results:

的第一列是比第二列窄。是否有我丢失的东西吗?我误解是如何工作的质量与网格布局?

The first column is narrower than the second column. Is there something I'm missing here? Am I misunderstanding how weight works with GridLayout?

至于另外一个问题,我似乎无法做到0宽度重招,以确保相同的宽度,无论内容(TextView的只是消失)。我怎样才能做到这一点与网格布局?

As another question, I can't seem to do the 0 width with weight trick to ensure equal width regardless of content (the TextView just disappears). How can I accomplish this with GridLayout?

推荐答案

只是添加机器人:layout_width =0dp在网格布局子视图。 并确保没有孩子认为具有比平均宽度尺寸更大的宽度尺寸,如果您有指定的宽度大小子视图。

just add android:layout_width="0dp" in GridLayout child view . and make sure there is no child view has a bigger width size than average width size if you have child view with specify width size .

希望这将帮助ü。

这篇关于Android的网格布局API 21的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆