D
D
Dmitry Eremin2017-04-02 18:02:53
Android
Dmitry Eremin, 2017-04-02 18:02:53

How to arrange images in three rows in Android Studio?

There are n-th number of square pictures of the same size. It is necessary to place them in three rows so that they occupy (almost) the entire width, with the exception of indents and spacing, which should be the same both vertically and horizontally. Simply put, you need to make something like a gallery, only with small indents. How to do it? Tried through GridView, but there you need to adjust the size. I set it up for Nexus 5, but everything has already gone to Nexus S, so it's not an option.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AndroidApi, 2017-04-02
@AndroidApi

GridView is just for this.
layout code

<GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:numColumns="3"
        android:verticalSpacing="2dp"
        android:horizontalSpacing="2dp"
        android:stretchMode="columnWidth" />

item
<GridViewItem
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView"
            android:scaleType="centerCrop"/>

And we predefine the onMeasure method so that the height and width of the image are equal:
public class GridViewItem extends ImageView{

    public GridViewItem(Context context) {
        super(context);
    }

    public GridViewItem(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GridViewItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}

And everything should be equal, and the empty space created between the cells should go to increase the cell itself.

E
Eugene, 2017-04-03
@UDZHEN

If as in the gallery, then try to do it using RecyclerView and CardView
Here is an example of how to implement https://github.com/tutsplus/Android-CardViewRecycl...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question