Gridview not fit for all screens in Android

后端 未结 2 654
迷失自我
迷失自我 2020-12-22 13:14

I am working in an Android app. In this I have an issue that my GridView and images are not get fit with all screen sizes .

Here I have the grid

相关标签:
2条回答
  • 2020-12-22 13:30

    Use GridLayout to acquire the desired result.Firstly add gridLayout dependancy:

    compile 'com.android.support:gridlayout-v7:27.0.1'
    

    Then, add the following xml code:

     <android.support.v7.widget.GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:columnCount="3">
    
        <FrameLayout
            app:layout_columnWeight="1"
            app:layout_rowWeight="1">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@android:drawable/ic_lock_idle_alarm"
                    android:layout_marginBottom="5dp"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Alarms"/>
            </LinearLayout>
    
         </FrameLayout>
    
      </android.support.v7.widget.GridLayout>
    

    In the above code, I've just added 1 element. Just copy & paste this FrameLayout and change texts & images accordingly. After adding 12 elements, the output should look as below:

    0 讨论(0)
  • 2020-12-22 13:33

    Try the follwoing GridLayout,

    <android.support.v7.widget.GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            app:alignmentMode="alignBounds"
            app:columnCount="3"
            app:rowCount="5">
    
        </android.support.v7.widget.GridLayout>
    

    And change your GridLayout item to the following,

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="1.5dp"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="5dp"
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            app:layout_rowWeight="1">
    
            <ImageView
                android:id="@+id/mImageView"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_gravity="center" />
    
            <TextView
                android:id="@+id/mTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:textAlignment="center"
                android:textSize="10dp" />
    
        </LinearLayout>
    

    To use the support.v7.widget.GridLayout add the dependency,

    implementation 'com.android.support:cardview-v7:27.0.1'

    Haven't tested yet, but it should work.

    0 讨论(0)
提交回复
热议问题