【问题标题】:Centralise last row of GridView [duplicate]集中 GridView 的最后一行 [重复]
【发布时间】:2018-10-01 12:06:47
【问题描述】:

我有一个动态的GridView,它应该显示从服务器获取的数据。该数据的长度是可变的。我设置了android:numColumns="4"。现在,如果数据的长度可以被 4 整除,那么我的布局如下所示:

这就是它应该的样子。但是,如果数据的长度不能被 4 整除而是偶数(形式为 4k+2),那么布局如下所示:

我查看了this StackOverflow 问题,很多人建议插入虚拟元素来欺骗 GridView 留下空白空间。这似乎是一个 hack,但我确实实现了我想要的布局:

现在,当数据长度为奇数时,就会出现问题。我默认得到的布局如下:

我想要得到这样的东西:

请注意,前面的解决方法在这里不起作用,因为没有一个完整的额外单元格可以插入虚拟对象。

我可以想出的一个解决方案是将列数更改为 7 并用虚拟节点替换每个备用单元格,除了第一个单元格也是虚拟节点的最后一行。这给了我类似的东西:

即使去掉horizontalSpacing,这些单元格之间的距离也太远了,代码的可读性已经严重受损。现在,我不是在寻找可以减小虚拟单元大小的解决方法。相反,我也想摆脱虚拟单元,因为在我看来,这不是一种有效的方法。

我已经查看了documentationGridView,但我找不到与我的问题相关的任何内容。除了上面链接的问题之外,我还查看了 thisthis StackOverflow 问题,但似乎没有一个问题有有效的答案。我还找到了this GitHub 存储库,作者声称已经修复了这个问题,但我无法重现它。

我很乐意从我的项目中提供代码 sn-ps,但我不认为它们会有用,因为:

  • 我正在寻找通用解决方案,而不是针对我的特定情况。
  • 我的项目并不像在 GridView 中放置字母那么简单,它包含许多与此问题无关的额外细节,因此已被删除以避免混淆。

【问题讨论】:

标签: android android-layout gridview


【解决方案1】:

下面的线性布局文件像网格布局一样动态变化。当您的要求不支持网格布局时,您可以尝试此操作。您可以找到该模式并使用此 weightSumlayout_weight 属性创建任何类型的对齐,这些属性会根据 android 设备动态变化。

**我删除了这段代码中的一些android:layout_width="match_parent" android:layout_height="match_parent" 属性,因为超过了最大字数。请把这两个属性放在线性布局标签中所有缺失的地方。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="6">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="5">

            <!-- first row -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="4">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:layout_weight="3">

                    <!-- 1st column -->

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:text="A"/>

                </LinearLayout>
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_weight="1"
                    android:weightSum="3">

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_weight="2">

                        <!-- second column -->

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:gravity="center"
                            android:text="B"/>

                    </LinearLayout>
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_weight="1"
                        android:weightSum="2">

                        <LinearLayout
                            android:orientation="horizontal"
                            android:layout_weight="1">

                            <!-- third column -->

                            <TextView
                                android:layout_gravity="center"
                                android:gravity="center"
                                android:text="C"/>

                        </LinearLayout>

                        <LinearLayout

                            android:orientation="horizontal"
                            android:layout_weight="1">

                            <!-- fourth column -->
                            <TextView

                                android:layout_gravity="center"
                                android:gravity="center"
                                android:text="D"/>

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

        <LinearLayout

            android:orientation="vertical"
            android:layout_weight="1"
            android:weightSum="5">

            <LinearLayout

                android:orientation="vertical"
                android:layout_weight="4">

                <!-- second row -->

                <LinearLayout

                    android:orientation="horizontal"
                    android:weightSum="4">

                    <LinearLayout

                        android:orientation="horizontal"
                        android:layout_weight="3">

                        <!-- 1st column -->

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:gravity="center"
                            android:text="A"/>

                    </LinearLayout>
                    <LinearLayout

                        android:orientation="horizontal"
                        android:layout_weight="1"
                        android:weightSum="3">

                        <LinearLayout

                            android:orientation="horizontal"
                            android:layout_weight="2">

                            <!-- second column -->

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_gravity="center"
                                android:gravity="center"
                                android:text="B"/>

                        </LinearLayout>
                        <LinearLayout

                            android:orientation="horizontal"
                            android:layout_weight="1"
                            android:weightSum="2">

                            <LinearLayout

                                android:orientation="horizontal"
                                android:layout_weight="1">

                                <!-- third column -->

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_gravity="center"
                                    android:gravity="center"
                                    android:text="C"/>

                            </LinearLayout>

                            <LinearLayout

                                android:orientation="horizontal"
                                android:layout_weight="1">

                                <!-- fourth column -->
                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_gravity="center"
                                    android:gravity="center"
                                    android:text="D"/>

                            </LinearLayout>

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_weight="1"
                android:weightSum="4">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:layout_weight="3">

                    <!-- third row -->

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal"
                        android:weightSum="4">

                        <LinearLayout

                            android:orientation="horizontal"
                            android:layout_weight="3">

                            <!-- 1st column -->

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_gravity="center"
                                android:gravity="center"
                                android:text="A"/>

                        </LinearLayout>
                        <LinearLayout

                            android:orientation="horizontal"
                            android:layout_weight="1"
                            android:weightSum="3">

                            <LinearLayout

                                android:orientation="horizontal"
                                android:layout_weight="2">

                                <!-- second column -->

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_gravity="center"
                                    android:gravity="center"
                                    android:text="B"/>

                            </LinearLayout>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="horizontal"
                                android:layout_weight="1"
                                android:weightSum="2">

                                <LinearLayout

                                    android:orientation="horizontal"
                                    android:layout_weight="1">

                                    <!-- third column -->

                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:text="C"/>

                                </LinearLayout>

                                <LinearLayout

                                    android:orientation="horizontal"
                                    android:layout_weight="1">

                                    <!-- fourth column -->
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:text="D"/>

                                </LinearLayout>

                            </LinearLayout>

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:layout_weight="1"
                    android:weightSum="3">

                    <LinearLayout

                        android:orientation="vertical"
                        android:layout_weight="2">

                        <!-- fourth row -->

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="horizontal"
                            android:weightSum="4">

                            <LinearLayout

                                android:orientation="horizontal"
                                android:layout_weight="3">

                                <!-- 1st column -->

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_gravity="center"
                                    android:gravity="center"
                                    android:text="A"/>

                            </LinearLayout>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="horizontal"
                                android:layout_weight="1"
                                android:weightSum="3">

                                <LinearLayout

                                    android:orientation="horizontal"
                                    android:layout_weight="2">

                                    <!-- second column -->

                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:text="B"/>

                                </LinearLayout>
                                <LinearLayout

                                    android:orientation="horizontal"
                                    android:layout_weight="1"
                                    android:weightSum="2">

                                    <LinearLayout

                                        android:orientation="horizontal"
                                        android:layout_weight="1">

                                        <!-- third column -->

                                        <TextView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_gravity="center"
                                            android:gravity="center"
                                            android:text="C"/>

                                    </LinearLayout>

                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:orientation="horizontal"
                                        android:layout_weight="1">

                                        <!-- fourth column -->
                                        <TextView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_gravity="center"
                                            android:gravity="center"
                                            android:text="D"/>

                                    </LinearLayout>

                                </LinearLayout>

                            </LinearLayout>

                        </LinearLayout>

                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:layout_weight="1"
                        android:weightSum="2">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical"
                            android:layout_weight="1">

                            <!-- fifth row -->

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="horizontal"
                                android:weightSum="4">

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:orientation="horizontal"
                                    android:layout_weight="3">

                                    <!-- 1st column -->

                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:text="A"/>

                                </LinearLayout>
                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:orientation="horizontal"
                                    android:layout_weight="1"
                                    android:weightSum="3">

                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:orientation="horizontal"
                                        android:layout_weight="2">

                                        <!-- second column -->

                                        <TextView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_gravity="center"
                                            android:gravity="center"
                                            android:text="B"/>

                                    </LinearLayout>
                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:orientation="horizontal"
                                        android:layout_weight="1"
                                        android:weightSum="2">

                                        <LinearLayout
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:orientation="horizontal"
                                            android:layout_weight="1">

                                            <!-- third column -->

                                            <TextView
                                                android:layout_width="match_parent"
                                                android:layout_height="match_parent"
                                                android:layout_gravity="center"
                                                android:gravity="center"
                                                android:text="C"/>

                                        </LinearLayout>

                                        <LinearLayout
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:orientation="horizontal"
                                            android:layout_weight="1">

                                            <!-- fourth column -->
                                            <TextView
                                                android:layout_width="match_parent"
                                                android:layout_height="match_parent"
                                                android:layout_gravity="center"
                                                android:gravity="center"
                                                android:text="D"/>

                                        </LinearLayout>

                                    </LinearLayout>

                                </LinearLayout>

                            </LinearLayout>

                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical"
                            android:layout_weight="1">

                            <!-- sixth row -->

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="horizontal"
                                android:weightSum="3">

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:orientation="horizontal"
                                    android:layout_weight="2"
                                    android:background="@color/colorPrimaryDark">

                                    <!-- first column -->

                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:text="U"/>

                                </LinearLayout>
                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:orientation="horizontal"
                                    android:layout_weight="1"
                                    android:weightSum="2">

                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:orientation="horizontal"
                                        android:layout_weight="1"
                                        android:background="@color/colorAccent">

                                        <!-- second column -->
                                        <TextView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_gravity="center"
                                            android:gravity="center"
                                            android:text="V"/>

                                    </LinearLayout>

                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:orientation="horizontal"
                                        android:layout_weight="1"
                                        android:background="@color/colorPrimary">

                                        <!-- third column -->

                                        <TextView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_gravity="center"
                                            android:gravity="center"
                                            android:text="W"/>


                                    </LinearLayout>

                                </LinearLayout>

                            </LinearLayout>

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>


        </LinearLayout>

    </LinearLayout>

</LinearLayout>

【讨论】:

    猜你喜欢
    • 2016-03-17
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    相关资源
    最近更新 更多