Android gridview android: numColumns = "auto_fit" always only create two columns

Hi, I am developing a small Android application in which I want to display a simple gridview with some elements. It is working correctly. The only problem is that it always shows only two columns, although there is space. It equally divides the screen into 2 columns and displays only two elements. If I set the number of columns as ie, rather than auto_fit, then it displays correctly. My code looks like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <GridView android:id="@+id/gridView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="5dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp"> </GridView> </FrameLayout> 

and my grid element is as follows:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:context="com.example.androidcardlayout.MainActivity" > <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_width="100dp" android:layout_height="150dp" card_view:cardCornerRadius="4dp"> <RelativeLayout android:id="@+id/cardMianRlt" android:layout_width="100dp" android:layout_height="150dp" > </RelativeLayout> 

Am I doing something wrong? Need help. Thanks.

+6
source share
1 answer

It seems like the unattended installation option only applies if you have fixed column widths set. Here is the only place in the GridView source code that uses the unattended setup option:

 private boolean determineColumns(int availableSpace) { final int requestedHorizontalSpacing = mRequestedHorizontalSpacing; final int stretchMode = mStretchMode; final int requestedColumnWidth = mRequestedColumnWidth; boolean didNotInitiallyFit = false; if (mRequestedNumColumns == AUTO_FIT) { if (requestedColumnWidth > 0) { // Client told us to pick the number of columns mNumColumns = (availableSpace + requestedHorizontalSpacing) / (requestedColumnWidth + requestedHorizontalSpacing); } else { // Just make up a number if we don't have enough info mNumColumns = 2; } } 

This private function is called during the measurement / layout process. Note that in the auto-fit if statement, except for requestedColumnWidth > 0 , you get only 2 columns, what you see.

If a fixed width works for your application, you need to put it in your XML as follows:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <GridView android:id="@+id/gridView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="5dp" android:numColumns="auto_fit" android:columnWidth="30dp" android:verticalSpacing="10dp" android:horizontalSpacing="10dp"> </GridView> </FrameLayout> 
+9
source

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


All Articles