I think your layout is fine.
The android:layout_width="match_parent" for the GridLayout , which is placed inside the Horizontal android:layout_width="match_parent" , is not affected. One child view inserted into the GridLayout determines the actual width of the GridLayout (height if you used a vertical ScrollView container), which may be larger than the width of the main screen (width = match_parent, so it has no effect here, as well as for child views). GridLayout columns and rows are the size of the largest child element inserted (assigned to this column / row).
All this is a very dynamic structure. The number of columns and rows is recalculated automatically. Remember to mark the children of layout_row and layout_column and possibly set the size you want - following the rules above, for example:
<EditText android:layout_width="200dp" android:layout_height="wrap_content" android:inputType="text" android:id="@+id/editText1" android:layout_row="2" android:layout_column="8" />
So, by changing the width of the child view, you can control the width of the columns of the GridLayout. You can study the following example:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:background="#f3f3f3"> <GridLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#a3ffa3"> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:text="Col 1,Row4" android:id="@+id/button" android:layout_gravity="center" android:layout_row="4" android:layout_column="1" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start.." android:layout_column="4" android:layout_row="8" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Col 6, Row 1." android:id="@+id/button2" android:layout_row="1" android:layout_column="6" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Col 6, Row 2." android:id="@+id/button3" android:layout_row="2" android:layout_column="6" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button4" android:layout_gravity="center" android:layout_column="9" android:layout_row="3" /> <TextView android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/textView" android:layout_row="3" android:layout_column="8" /> <CheckBox android:layout_width="250dp" android:layout_height="wrap_content" android:text="New CheckBox" android:id="@+id/checkBox" android:layout_row="6" android:layout_column="7" android:textColor="#212995" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="text" android:ems="10" android:id="@+id/editText" android:layout_row="5" android:layout_column="8" android:textColor="#952a30" /> </GridLayout> </HorizontalScrollView> </LinearLayout>
Hope this was helpful. Best wishes:)
Also: I found that the above can actually be difficult to implement programmatically. You might want to change the GridLayout to a GridView or TableLayout, which are easier to handle. To learn more about this, check out these sites:
source share