2016-03-10 111 views
2

我嘗試將我的所有項目居中放在GridLayout中,我嘗試了很多事情(使用RelativeLayout作爲父項,然後使用layout_centerInParent,使用基本的android:layout_gravity =「center」,嘗試添加一些空間元素......),但是我沒有成功將我的物品居中。作爲圖像他勝於言,這裏是我有:在GridLayout中水平居中項目

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或類似的東西得到這樣的結果?

Ps:SquareRelativeLayout只是一個簡單的CustomLayout,強制我的RelativeLayout是一個正方形(width = height)。

回答

0

你需要一個GridLayout嗎? 如果沒有,你可以嘗試這樣的事:

<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> 
+0

我的項目可以是可變的,我覺得它更好地使用該佈局的設計,我也許可以使用此解決方案,但我需要做一些編程技巧就能夠擁有好數量的LinearLayout。 – zed13

+0

最後,我用你的提示來構建我自己的佈局,謝謝。 – zed13