安卓网格布局 API 21

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

我正在尝试完成这样的事情:

enter image description here

目前,我手动将图 block 的宽度设置为屏幕宽度的一半。这很好用,但它使得在图 block 之间添加分隔线(如屏幕截图所示)变得困难。幸运的是,似乎在 API 21 中,there is now support for weight in GridLayout ,为方便起见,在此处引用:

As of API 21, GridLayout's distribution of excess space accomodates the principle of weight. In the event that no weights are specified, the previous conventions are respected and columns and rows are taken as flexible if their views specify some form of alignment within their groups. The flexibility of a view is therefore influenced by its alignment which is, in turn, typically defined by setting the gravity property of the child's layout parameters. If either a weight or alignment were defined along a given axis then the component is taken as flexible in that direction. If no weight or alignment was set, the component is instead assumed to be inflexible.

Multiple components in the same row or column group are considered to act in parallel. Such a group is flexible only if all of the components within it are flexible. Row and column groups that sit either side of a common boundary are instead considered to act in series. The composite group made of these two elements is flexible if one of its elements is flexible.

To make a column stretch, make sure all of the components inside it define a weight or a gravity. To prevent a column from stretching, ensure that one of the components in the column does not define a weight or a gravity.

When the principle of flexibility does not provide complete disambiguation, GridLayout's algorithms favour rows and columns that are closer to its right and bottom edges. To be more precise, GridLayout treats each of its layout parameters as a constraint in the a set of variables that define the grid-lines along a given axis. During layout, GridLayout solves the constraints so as to return the unique solution to those constraints for which all variables are less-than-or-equal-to the corresponding value in any other valid solution.

我将这个简单的布局放在一起,该布局由具有 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 完成此操作?

最佳答案

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

如果您有指定宽度的 subview ,请确保没有 subview 的宽度大于平均宽度。

希望对你有所帮助

关于安卓网格布局 API 21,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27718928/

相关文章:

android - 将 ListView 项目放在可见区域的中心

java - Jetty WebSockets ssl == null 错误

design-patterns - 是否有用于设计具有多个字段的搜索表单的良好 UI 模式?

java - 如何设置标题和段落间距?

javascript - 优雅降级到非 JavaScript UI 的重要性

android - 如何在缩放背景图像上放置按钮

java - 命名 AsyncTask 的线程

android - TextView 未显示在 RelativeLayout 中

android - 无法获取 Android View 的资源 ID

android - 有什么办法可以使表格列等于总宽度的一小部分?