Как сделать так, чтобы мой GridLayout находился в центре экрана - PullRequest
0 голосов
/ 03 октября 2018

Это мой макет Android.Я хочу отобразить таблицу с 3 рядами на 3 столбца.Каждая ячейка представляет собой квадратную (четную) кнопку.Как сделать так, чтобы центральный красный квадрат / кнопка находились в центре моего устройства Android?Я должен иметь эту сетку в центре моего экрана, а не сверху.

Код квадратной кнопки:

  public class SquareButton extends Button {
    public SquareButton(Context context) {
        super(context);
    }

    public SquareButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SquareButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int size = width > height ? height : width;
        setMeasuredDimension(size, size);
    }
}

И мой макет:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center">


   <GridLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:rowCount="3"
       android:columnCount="3"
       android:orientation="vertical"
       android:foregroundGravity="center">

       <LinearLayout>

           <com.example.talangel.stacktests.SquareButton
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="#FF0000"
               android:layout_weight="1"/>
           <com.example.talangel.stacktests.SquareButton
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"/>
           <com.example.talangel.stacktests.SquareButton
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="#0000FF"
               android:layout_weight="1"/>
       </LinearLayout>

       <LinearLayout>

           <com.example.talangel.stacktests.SquareButton
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="#0000FF"
               android:layout_weight="1"/>
           <com.example.talangel.stacktests.SquareButton
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="#FF0000"
               android:layout_weight="1"/>
           <com.example.talangel.stacktests.SquareButton
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"/>
       </LinearLayout>

       <LinearLayout>

           <com.example.talangel.stacktests.SquareButton
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"/>
           <com.example.talangel.stacktests.SquareButton
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="#0000FF"
               android:layout_weight="1"/>
           <com.example.talangel.stacktests.SquareButton
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="#FF0000"
               android:layout_weight="1"/>
       </LinearLayout>

   </GridLayout>


</RelativeLayout>

1 Ответ

0 голосов
/ 03 октября 2018

Вам нужно изменить свой код как ..

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...