Android GridLayout API 21

时间:2014-12-31 09:22:05

标签: android android-layout user-interface android-5.0-lollipop android-gridlayout

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

enter image description here

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

  

从API 21开始,GridLayout的多余空间分布可以容纳   重量原则。如果没有指定权重,   遵循以前的约定,并采用列和行   如果他们的观点在他们的观点中指定了某种形   组。因此,视图的灵活性受其影响   反过来,通常通过设定重力来定义   孩子的布局参数的属性。如果重量或   沿给定轴定义对齐,然后取出组件   在这方面是灵活的。如果没有设置重量或对齐,则   相反,组件被认为是不灵活的。

     

考虑同一行或列组中的多个组件   并行行事。这样一个群体只有在所有群体中都是灵活的   其中的组件是灵活的。坐的行和列组   相反,公共边界的任何一方都被认为是在行动   系列。由这两个元素组成的复合组是灵活的   其中一个要素是灵活的。

     

要使列拉伸,请确保其中的所有组件   定义重量或重力。为防止色谱柱拉伸,   确保列中的某个组件未定义a   重量或重力。

     

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

我将这个简单的布局组合在一起,其中包含一个包含2列的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的权重如何?

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

2 个答案:

答案 0 :(得分:6)

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

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

希望它会对你有所帮助。

答案 1 :(得分: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,我不得不玩eclipse 2分钟才能得到它..所以列和行...你可以实际玩它们,我只是做了一个列和我的例子中的另一列为empty.its。

希望有所帮助