集中GridView的最后一行

时间:2018-10-01 12:06:47

标签: android android-layout gridview

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

Screenshot for Number of cells divisible by 4

应该是这样。但是,如果数据长度不能被4整除,而是被偶数(格式为4k + 2),则布局看起来像这样:

Screenshot for Number of cells of the form 4k+2

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

Screenshot for Number of cells of the form 4k+2, centered

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

Screenshot for Number of cells odd

我想要得到这样的东西:

Screenshot for Number of cells odd

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

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

Screenshot for Number of cells odd, with 7 columns

即使摆脱了horizontalSpacing,这些单元也相距太远,并且代码的可读性也受到严重损害。现在,我不是在寻找可以减少虚拟单元格大小的解决方法。相反,我也想摆脱虚拟单元格,因为我认为这不是一种有效的方法。

我在GridView中经历过documentation,在这里我找不到与问题有关的任何东西。除了上面链接的问题之外,我还研究了thisthis StackOverflow问题,但是似乎都没有一个有效的答案。我还找到了this GitHub存储库,作者声称已在其中修复了此问题,但我无法复制它。

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

  • 我正在寻找通用解决方案,而不是针对我的特定情况。
  • 我的项目并不像将字母放入GridView一样简单,它包含许多与该问题无关的额外细节,因此为了避免混淆,将其删除。

1 个答案:

答案 0 :(得分:0)

下面的线性布局文件像网格布局一样动态变化。当您的要求不支持网格布局时,可以尝试此操作。您可以找到该模式并使用此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>