D
D
davidnum952014-10-02 11:39:32
Android
davidnum95, 2014-10-02 11:39:32

How to lay out a grid in android studio?

I want to arrange the elements like this:
custom-gridview.png
and it turns out like this: the
e5f79b589c2942ca94d1190db6914b7d.png
main_activity.xml file:

<RelativeLayout 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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".my"
    android:background="@color/bg_color">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/date"
        android:id="@+id/textViewDate"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/prevButton"
        android:src="@android:drawable/ic_media_previous"
        android:layout_toStartOf="@+id/textViewDate"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/textViewDate"
        android:layout_marginRight="37dp"
        android:layout_alignBottom="@+id/textViewDate"
        android:clickable="false"
        android:onClick="onClickPrev" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/nextButton"
        android:layout_toEndOf="@+id/textViewDate"
        android:src="@android:drawable/ic_media_next"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textViewDate"
        android:layout_marginLeft="40dp"
        android:layout_alignBottom="@+id/textViewDate"
        android:onClick="onClickNext" />

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/customGrid"
        android:layout_below="@+id/prevButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:numColumns="auto_fit"
        android:layout_toStartOf="@+id/prevButton"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

item.xml:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/item_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </ImageView>

    <TextView
        android:id="@+id/item_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TextView>

</LinearLayout>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
one pavel, 2014-10-02
@davidnum95

<GridView
            a:id="@+id/gridView"
            a:layout_width="fill_parent"
            a:layout_height="fill_parent"
            a:columnWidth="90dp"
            a:numColumns="auto_fit"
            a:verticalSpacing="10dp"
            a:horizontalSpacing="10dp"
            a:stretchMode="columnWidth"
            a:gravity="center">

A
Artix, 2014-10-02
@nikkorejz

Use Gridview. Everything is detailed in the documentation. Can be set via XML and also via JAVA code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question