Android GridLayout API 21

unt*_*led 6 user-interface android android-layout android-gridlayout android-5.0-lollipop

我正在努力完成这样的事情:

在此输入图像描述

目前,我手动设置瓷砖的宽度等于屏幕宽度的一半.这样做效果很好,但是它会在拼贴之间添加分隔线(如屏幕截图所示).幸运的是,似乎在API 21中,现在支持GridLayout中的权重,为方便起见,这里引用了:

从API 21开始,GridLayout的多余空间分布适应了权重原则.如果未指定权重,则遵循先前的约定,并且如果列和行在其组中指定某种形式的对齐,则将列和行视为灵活的.因此,视图的灵活性受其对齐的影响,而对齐通常通过设置孩子的布局参数的重力属性来定义.如果沿给定轴定义了重量或对齐,则该组件在该方向上被视为柔性.如果没有设置重量或对齐,则假定组件不灵活.

同一行或列组中的多个组件被视为并行操作.只有当其中的所有组件都是灵活的时,这样的组才是灵活的.位于公共边界两侧的行和列组被认为是串行操作.如果其中一个元件是柔性的,则由这两个元件组成的复合材料组是柔性的.

要使柱伸展,请确保其中的所有组件都定义了重量或重力.要防止色谱柱拉伸,请确保色谱柱中的某个组分未定义重量或重力.

当灵活性原则不能提供完全消歧时,GridLayout的算法更倾向于靠近其右边和底边的行和列.更确切地说,GridLayout将其每个布局参数视为一组变量中的约束,这些变量定义了沿给定轴的网格线.在布局期间,GridLayout解决约束,以便将唯一解决方案返回到那些约束,其中所有变量都小于或等于任何其他有效解决方案中的相应值.

我将这个由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>
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

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

小智 6

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

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

希望它会对你有所帮助.


Ell*_*ltz 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>
Run Code Online (Sandbox Code Playgroud)

按钮上的文字解释了如何gridlayout工作,非常酷,对吧?

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

实际上从来没有花时间gridlayout,我不得不玩 Eclipse 2 分钟才能得到它..所以列和行..实际上你可以玩它们,我刚刚做了,有一列和另一列是空的。它在我的例子。

希望能帮助到你