水平居中内Android的网格布局查看网格、布局、水平、Android

2023-09-11 07:52:58 作者:陈英俊!

我们正在编写针对ICS的应用程序+,相信一个网格布局是最好的布局模式,但似乎很少被写了关于它,我们有一些调整问题。

We are writing an app targeting ICS+ and believe a GridLayout is the best layout paradigm, but it seems very little has been written about it, and we are having some alignment issues.

<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row_background"
    android:rowCount="1"
    android:columnCount="3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:useDefaultMargins="true"
    android:background="@drawable/list_item_bg">

    <ImageView
        android:id="@+id/visibilityIcon"
        android:layout_row="0"
        android:layout_column="0"
        android:src="@drawable/visibility_icon" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>

    <ImageView
        android:id="@+id/windIcon"
        android:layout_row="0"
        android:layout_column="1"
        android:src="@drawable/wind_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>

    <ImageView
        android:id="@+id/crosswindIcon"
        android:layout_row="0"
        android:layout_column="2"
        android:src="@drawable/cloud_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>

</GridLayout>

但是,左侧2个图标保持左对齐,并与剩余的空间最右图标中心

However, the left 2 icons remain left-aligned, and the right-most icon centers with the remaining space.

从本质上讲,我们需要做的是指定每个列的大小为1/3(因为3列)总的屏幕大小。我想这就是网格布局做了,但它似乎WRAP_CONTENT导致出现此问题(是有道理的),而是match_parent导致第一栏填满整个屏幕,而不是填补它的细胞,其是我本来期望的行为。

Essentially what we need to do is specify the size of each column to be 1/3 (since 3 columns) of the total screen size. I thought this is what GridLayout did, but it appears 'wrap_content' causes this behavior (makes sense), but 'match_parent' causes the first column to fill the entire screen, rather than fill its cell which is the behavior I would have expected.

我们似乎都试过重力,layout_gravity等的每一个组合,但无论是从根本上正在做的事情错了,或者已经找到了网格布局的限制。

We seem to have tried every combination of gravity, layout_gravity, etc., but either we fundamentally are doing something wrong, or have found a limitation of the GridLayout.

感谢您的帮助!

推荐答案

只有一行和一列可以在GridLayout增长,这是一个与沿该轴重力。如果多于一行或一列指定的重力只有一个会得到它(如果我记得它是最后一节)。选择其他布局或自己编写。如果你只想与平分秋色图标一排,你可以使用的LinearLayout其中的各个部件的宽度0px和重量都是一样的,例如: 1。

Only one row and one column is allowed to grow in a GridLayout, and that is the one with gravity along that axis. If more than one row or column specify gravity only one will get it (if I remember it is the "last" one). Choose another layout or write your own. If you only want a row with equally split icons you can use a LinearLayout where the widths of the components are 0px and the weight are all the same, e.g. 1.