2016-03-10 3 views
2

Я пытаюсь сосредоточить все мои объекты в своем GridLayout, я пробовал много вещей (используйте RelativeLayout в качестве родителя, затем используйте layout_centerInParent, используйте базовый андроид: 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 или что-то подобное?

Пс: 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

Смежные вопросы