网格布局(不GridView控件)如何扩展所有儿童均匀网格、控件、均匀、布局

2023-09-11 11:45:46 作者:不甘平庸

我想有一个2x2的网格,一个按键内。这仅仅是ICS,所以我想用给定的新的网格布局。

I want to have a 2x2 grid with a buttons inside. This is only ICS so I am trying to use the new GridLayout given.

下面是我的布局的XML:

Here's the XML of my layout:

 <?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/favorites_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00"
    android:rowCount="2"
    android:columnCount="2">
  <Button
      android:text="Cell 0"
      android:layout_row="0"
      android:layout_column="0"
      android:textSize="14dip" />
  <Button
      android:text="Cell 1"
      android:layout_row="0"
      android:layout_column="1"
      android:textSize="14dip" />

  <Button
      android:text="Cell 2"
      android:layout_row="1"
      android:layout_column="0"
      android:textSize="14dip" />
  <Button
      android:text="Cell 3"
      android:layout_row="1"
      android:layout_column="1"
      android:textSize="14dip" />
</GridLayout>

现在的问题是,我的看法没有均匀地伸展的每一行。这会导致大量的额外空间,我的网格布局的权利。

The problem is that my views do not stretch evenly for each row. This causes a lot of extra space to the right of my GridLayout.

我试着设置 layout_gravity =fill_horizo​​ntal,但只适用于在最后一个的行视图。这意味着电池1绵延一路给予足够的空间单元0。

I tried setting layout_gravity="fill_horizontal" but that only applies to the last view on the row. This means Cell 1 stretches all the way to give enough space for Cell 0.

在如何解决这个想法?

推荐答案

更​​新:权重都为21 API的更多详细信息,请参阅PaulT的回答支持

UPDATE: Weights are supported as of API 21. See PaulT's answer for more details.

有使用网格布局时的局限性,以下报价是从的文档。

There are limitations when using the GridLayout, the following quote is taken from the documentation.

网格布局不为权重的原则,提供支持,   在重定义。在一般情况下,它是不因此能够   配置网格布局,以在不平凡的分配剩余空间   多行或列的比例。对于完全控制   在行或列的剩余空间分布;使用的LinearLayout   子视图保持部件相关联的单元组中。

"GridLayout does not provide support for the principle of weight, as defined in weight. In general, it is not therefore possible to configure a GridLayout to distribute excess space in non-trivial proportions between multiple rows or columns ... For complete control over excess space distribution in a row or column; use a LinearLayout subview to hold the components in the associated cell group."

下面是一个使用的LinearLayout子视图的一个小例子。 (我用的空间视图,占用未利用地面积和推按钮到所需的位置。)

Here is a small example that uses LinearLayout subviews. (I used Space Views that takes up unused area and pushes the buttons into desired position.)

<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="1"
>
    <TextView
        android:text="2x2 button grid"
        android:textSize="32dip"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="Button 2" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
    >
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="Button 4" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
</GridLayout>