Here is a clumsy grid turned out enter image description here

I would like the letters and numbers to stand exactly, I do not understand what is wrong? Trying, trying, failing

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical" android:weightSum="100" android:background="#ffffff" android:paddingRight="50dp" android:paddingLeft="50dp"> <LinearLayout android:layout_width="0dp" android:layout_weight="50" android:layout_height="wrap_content" android:layout_marginRight="40dp" android:orientation="vertical"> <include layout="@layout/characters"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/numbers"/> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:id="@+id/gameTablePlayer1"/> </LinearLayout> </LinearLayout> <RelativeLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:layout_weight="50"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:overScrollMode="never" android:id="@+id/gameTablePlayer2"/> </RelativeLayout> </LinearLayout> 

characters

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <ru.albatros.buttleshit.TextViewSquare android:layout_width="match_parent" android:layout_weight="1" android:gravity="center" android:layout_height="wrap_content" android:textColor="#000000" android:text="А"/> <ru.albatros.buttleshit.TextViewSquare android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#000000" android:text="B"/> <ru.albatros.buttleshit.TextViewSquare android:layout_weight="1" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="C"/> <ru.albatros.buttleshit.TextViewSquare android:layout_weight="1" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="D"/> <ru.albatros.buttleshit.TextViewSquare android:gravity="center" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="E"/> <ru.albatros.buttleshit.TextViewSquare android:gravity="center" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="F"/> <ru.albatros.buttleshit.TextViewSquare android:gravity="center" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="G"/> <ru.albatros.buttleshit.TextViewSquare android:gravity="center" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="H"/> <ru.albatros.buttleshit.TextViewSquare android:layout_weight="1" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="I"/> <ru.albatros.buttleshit.TextViewSquare android:layout_weight="1" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="J"/> </LinearLayout> 

numbers

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="match_parent"> <ru.albatros.buttleshit.TextViewSquareX android:layout_width="wrap_content" android:layout_weight="1" android:gravity="center" android:layout_height="match_parent" android:textColor="#000000" android:text="1"/> <ru.albatros.buttleshit.TextViewSquareX android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:textColor="#000000" android:text="2"/> <ru.albatros.buttleshit.TextViewSquareX android:layout_weight="1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="#000000" android:text="3"/> <ru.albatros.buttleshit.TextViewSquareX android:layout_weight="1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="#000000" android:text="4"/> <ru.albatros.buttleshit.TextViewSquareX android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="#000000" android:text="5"/> <ru.albatros.buttleshit.TextViewSquareX android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="#000000" android:text="6"/> <ru.albatros.buttleshit.TextViewSquareX android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="#000000" android:text="7"/> <ru.albatros.buttleshit.TextViewSquareX android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="#000000" android:text="8"/> <ru.albatros.buttleshit.TextViewSquareX android:layout_weight="1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="#000000" android:text="9"/> <ru.albatros.buttleshit.TextViewSquareX android:layout_weight="1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="#000000" android:text="10"/> </LinearLayout> 

TextViewSquare

 public class TextViewSquare extends TextView { public TextViewSquare(Context context) { super(context); } public TextViewSquare(Context context, AttributeSet attrs) { super(context, attrs); } public TextViewSquare(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, widthMeasureSpec); } } 

TextViewSquareX

 public class TextViewSquareX extends TextView { public TextViewSquareX(Context context) { super(context); } public TextViewSquareX(Context context, AttributeSet attrs) { super(context, attrs); } public TextViewSquareX(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(heightMeasureSpec, heightMeasureSpec); } } 

Added an empty TextView before the letter A, that's what happened enter image description here

  • In the vertical line-out you need to put the letters and the horizontal line-out, in which the numbers and the field, align everything using the margin and padding. And weight is not interest, it is not necessary to bet on 50, and you can also on 1. startandroid.ru/ru/uroki/vse-uroki-spiskom/… - Nikolai Konorev
  • Paddings / margins do not roll, my dependencies on the screens are set - Flippy
  • The joke is that I stretch 10 view in characters and numbers , respectively, the column with numbers is also considered as part and stretches not along the side of the grid but along the side of the grid + the width of the column with numbers. If you make margin, then you need to know how much, but I do not know, because it all depends on the screen - Flippy
  • How inconvenient to do these markup - Flippy
  • and you did not look towards the GridView? If you know the size of your cell, then nothing needs to be aligned! - Ivan Vovk

1 answer 1

Look here: take the side of the square 40 for example. With all the text we set 40dp width and height and the same parameters to the gridlayout cells. Here is an example of the layout structure:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/letters_width" android:paddingLeft="@dimen/letters_width"> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="1"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="2"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="3"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:layout_width="@dimen/letters_width" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="a"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="b"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="c"/> </LinearLayout> <ListView android:layout_width="120dp" android:layout_height="120dp"/> </LinearLayout> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/letters_width" android:paddingLeft="@dimen/letters_width"> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="1"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="2"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="3"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:layout_width="@dimen/letters_width" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="a"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="b"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="c"/> </LinearLayout> <ListView android:layout_width="120dp" android:layout_height="120dp"/> </LinearLayout> </LinearLayout> 

enter image description here

ListView need only be changed to your recyclerview, make the right number of letters and numbers and fit the size. You can also use Include instead of duplication.

  • The problem is that 40dp does not guarantee the correct size. It is necessary to cope with weight. I can't do it even with RelativeLayoyt - Flippy
  • Just include I use - Flippy
  • I still have a lot of twist so ... Damn. I give up. Make a contest. - Flippy
  • edited with weight. @ dimen / letters_width specifies the width of the text with letters and the height with numbers. - Nikolai Konorev
  • In general, tomorrow I will turn on the full brain and do it. I can not specify the specific dimensions, the whole game will be without them, only the weight - Flippy