代码之家  ›  专栏  ›  技术社区  ›  zed13

在GridLayout中水平居中项目

  •  3
  • zed13  · 技术社区  · 10 年前

    我尝试在GridLayout中居中我的所有项目,我尝试了很多事情(使用RelativeLayout作为父级,然后使用layout_centerInParent,使用基本的android:layout_gravity=“center”,尝试添加一些空间元素…),但我没有成功地将项目居中。作为一个形象,他胜过文字,以下是我所拥有的:

    enter image description here

    这就是我想要的:

    enter image description here

    我现在的代码是:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/mainContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="MainActivity">
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#66000000">
    
            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="0dp" ... />
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.50">
    
            <android.support.v7.widget.GridLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:grid="http://schemas.android.com/apk/res-auto"
                android:id="@+id/droparea"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.50"
                grid:columnCount="3"
                grid:orientation="horizontal">
    
                <SquareRelativeLayout
                    android:id="@+id/relativelayout"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    grid:layout_columnWeight="1"
                    grid:layout_gravity="fill"
                    android:background="@drawable/round_layout"
                    android:gravity="center_vertical|center_horizontal">
                    ...
                </SquareRelativeLayout>
    
                <SquareRelativeLayout
                    android:id="@+id/relativelayout2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    grid:layout_columnWeight="1"
                    grid:layout_gravity="fill"
                    android:background="@drawable/round_layout"
                    android:gravity="center_vertical|center_horizontal">
                    ...
                </SquareRelativeLayout>
    
                <SquareRelativeLayout
                    android:id="@+id/relativelayout3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    grid:layout_columnWeight="1"
                    grid:layout_gravity="fill"
                    android:background="@drawable/round_layout"
                    android:gravity="center_vertical|center_horizontal"> 
                    ...
                </SquareRelativeLayout>
    
                <SquareRelativeLayout
                    android:id="@+id/relativelayout4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    grid:layout_columnWeight="1"
                    grid:layout_gravity="fill"
                    android:background="@drawable/round_layout"
                    android:gravity="center_vertical|center_horizontal">
                    ....
                </SquareRelativeLayout>
    
                <SquareRelativeLayout
                    android:id="@+id/relativelayout5"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    grid:layout_columnWeight="1"
                    grid:layout_gravity="fill"
                    android:background="@drawable/round_layout"
                    android:gravity="center_vertical|center_horizontal">
                    ...
                </SquareRelativeLayout>
    
                </android.support.v7.widget.GridLayout>
            </RelativeLayout>
    
            <include layout="@layout/draggable_linearlayout"/>
    
        </LinearLayout>
    
    </RelativeLayout>
    

    你有什么想法吗,我如何用GridLayout或类似的东西得到这样的结果?

    注意:SquareRelativeLayout只是一个简单的CustomLayout,它强制我的RelativeLayout为正方形(宽度=高度)。

    1 回复  |  直到 10 年前
        1
  •  -2
  •   ZelvaJan    10 年前

    你需要网格布局吗? 如果没有,您可以尝试以下操作:

        <LinearLayout
            android:id="@+id/firstRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3">
    
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="@color/white"/>
    
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="@color/white"/>
    
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="@color/white"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/SecondRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:weightSum="3">
    
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="@color/white"/>
    
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="@color/white"/>
        </LinearLayout>