Android GridLayout API 21

前端之家收集整理的这篇文章主要介绍了Android GridLayout API 21前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在努力完成这样的事情:

目前,我手动设置瓷砖的宽度等于屏幕宽度的一半.这样做效果很好,但是它会在拼贴之间添加分隔线(如屏幕截图所示).幸运的是,似乎在API 21,there is now support for weight in GridLayout中引用了这里以方便您:

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.

我将这个由GridLayout和2列组成的简单布局放在一起,尝试实现两个同等宽度的列:

<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>

我认为,因为所有的孩子都有相同的柱重,所以会产生同样宽的柱子.然而,情况似乎并非如此,因为这是结果:

第一列比第二列窄.这里有什么我想念的吗?我是否误解了GridLayout的重量如何?

作为另一个问题,我似乎不能用重量技巧做0宽度以确保相同的宽度而不管内容(TextView只是消失).如何使用GridLayout完成此操作?

解决方法

只需将android:layout_width =“0dp”添加到GridLayout的每个子节点(任何没有layout_width =“0dp”的子节点都会破坏权重布局.)

如果您具有指定宽度大小的子视图,请确保没有子视图的宽度大于平均宽度大小.

希望它会对你有所帮助.

原文链接:https://www.f2er.com/android/308648.html

猜你在找的Android相关文章