简体   繁体   中英

How to create GridView with squares items on two columns?

I would like to create a GridView with squares items on two columns which would take the totality of the screen (match_parent).

Every item consists of an imageView and a textView.

Here is the result in image :

在此处输入图片说明

How do I make the items width to be equal to their variable height?

Thank you in advance!!

In your activity or Fragment layout you should have GridView like this:

<GridView
        android:id="@+id/grid_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
         />

And then you should have an Adapter that extends BaseAdapter for example and it's item should be as follow:

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawableTop="@drawable/yourDrawable"
        android:layout_margin="20dip"
        android:gravity="center" />

You need more explanation ?

Use these link, they will help you Option 1

Option 2

This is Gridview.

<GridView
    android:numColumns="2"
    android:id="@+id/related_contact_grid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

This is item. You will inflate this to gridview in Adapter.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView"
        android:text="Tendance"
        android:layout_centerHorizontal="true"

</RelativeLayout>

If you want, I can support your work on this Gridview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM