Why doesn't GridLayout fill all the free space?

I have a GridLayout (to which I add children programmatically).

The result is ugly because GridLayout does not fill all the free space.

This is the result:

enter image description here

this is my xml:

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.GridLayout android:id="@+id/gridlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" > </android.support.v7.widget.GridLayout> </HorizontalScrollView> </ScrollView> 
+6
source share
6 answers

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:

+7
source

You have to change your

 <HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent" > 

and add android:fillViewPort="true" . This should stretch the content to fill the screen (i.e., the viewport).

+4
source

TextView reps = new TextView(this); android.support.v7.widget.GridLayout.LayoutParams repsLayoutParam = (LayoutParams) reps.getLayoutParams(); repsLayoutParam.setGravity(Gravity.FILL_HORIZONTAL | Gravity.FILL_VERTICAL);

This creates a text view that fits into the grid layout - setting gravity to FILL_HORIZONTAL, as well as FILL_VERTICAL should ensure that it fills the cell in the layout.

+2
source

Since the grid has its own scroll.

+1
source

When you add views, be sure to add the right LayoutParams to the view first.

+1
source

Mabye is due to lack of

 </ScrollView> 

in the end.

0
source

Source: https://habr.com/ru/post/954276/


All Articles