Android GridLayout API 21

6

我正在尝试实现类似这样的功能:

enter image description here

目前,我手动将瓷砖的宽度设置为屏幕宽度的一半。 这很有效,但在瓷砖之间添加分隔符(如屏幕截图中所示)很困难。 幸运的是,在API 21中,GridLayout现在支持权重,这里为您方便引用:

从API 21开始,GridLayout对多余空间的分配采用了权重原则。如果没有指定权重,则尊重以前的约定,即如果视图在其组内指定了某种对齐方式,则列和行将被视为可变。因此,视图的灵活性受其对齐方式的影响,而通常是通过设置子布局参数的gravity属性来定义的。如果给定轴线上定义了权重或对齐方式,则将该组件视为在该方向上具有灵活性。如果没有设置权重或对齐方式,则该组件被认为是不灵活的。 同一行或列组中的多个组件被认为是并行的。只有当组内所有组件都是灵活的时,这样的组才是灵活的。相邻的行和列组被视为串联。由这两个元素组成的复合组件在其中一个元素是灵活的情况下也是灵活的。 要使列拉伸,请确保其内部的所有组件都定义了权重或gravity。要防止列拉伸,请确保列中的一个组件没有定义权重或gravity。 当灵活性原则无法提供完全消除歧义时,GridLayout算法偏爱靠近右侧和底部边缘的行和列。更准确地说,GridLayout将其每个布局参数视为沿给定轴线定义网格线的变量集中的约束。在布局期间,GridLayout解决约束,以便返回这些约束的唯一解,其中所有变量都小于或等于任何其他有效解中相应的值。

我设计了一个简单的布局,包含两列网格布局。试图实现两个等宽的列:

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

我认为因为所有的子元素具有相等的列宽比重,所以会导致等宽的列。然而实际情况并非如此,因为以下结果是这样的:

enter image description here

第一列比第二列窄。我是否遗漏了什么?我是否误解了GridLayout中的权重工作原理?
另一个问题是,我似乎无法使用权重技巧来确保相等的宽度,而不考虑内容(TextView会消失)。我如何在GridLayout中实现这一点?
2个回答

6

只需在GridLayout的每个子元素中添加android:layout_width="0dp"(没有layout_width="0dp"的任何子元素都会破坏权重布局)。

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

希望对您有所帮助。


2

请试一下,我很快就会编辑它,想自己尝试并查看。

  <GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"        
    android:columnCount="2" > 

    <Button
        android:layout_column="0"
        android:layout_gravity="fill"
        android:layout_row="0"
        android:layout_columnWeight="1"
        android:text="c0 | r0" />

   <Button
        android:layout_column="1"
        android:layout_gravity="fill"
        android:layout_row="0"
         android:layout_columnWeight="1"
        android:text="c1 | r0" />

   <Button
        android:layout_column="1"
        android:layout_gravity="fill"
        android:layout_row="3"
        android:layout_columnWeight="1"
        android:text="c1 | r3" />

   <Button
        android:layout_column="0"
        android:layout_gravity="fill"
        android:layout_row="1"
        android:layout_columnWeight="1"
        android:text="c0 | r1" />

</GridLayout>

按钮上的文本解释了gridlayout的工作原理,很酷对吧?

您可以为行指定列数,并将小部件放置在其各自的位置(列号和行号).. 在文本中放置位置以反映它..

事实上,我从未花时间研究过gridlayout,我只玩了2分钟的eclipse就搞定了.. 所以列和行..你可以随时调整它们,我只是随意调整了一下,有一列是空的,在我的示例中。

希望能有所帮助


那似乎解决了问题。有没有办法在不指定行/列的情况下使用权重?我希望能够只添加子项,而无需跟踪当前行/列,因为我的某些子项可能跨越多行/列。 - untitled
抱歉,但从我看到的情况来看,这就是网格布局的工作方式(单元格),所以你可以使用 android:layout_gravity="Left|Top|fill"android:layout_gravity="Right|Top|fill" 来实现。也许这样会起作用。但老实说,我不认为这就是网格布局的工作方式。最后,你总是可以通过 findviewById 找到你的视图,因为它是一个视图组,单元格是用于对齐的。希望能有所帮助。@untitled - Elltz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接