Как сделать фиксированную высоту для элементов в GridLayoutManager? - PullRequest
0 голосов
/ 13 марта 2019

Я делаю сетку с изображением и текстом, используя GridLayoutManager. Но в настоящее время мои элементы имеют разную высоту, если в одной строке текст находится в одной строке, а во второй строке - две строки, что выглядит не очень хорошо. Как сделать все элементы равными друг другу (лучший вариант, если высота каждого элемента будет равна максимальной высоте элемента

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/white"
    android:layout_margin="1dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textAlignment="center"
            tools:text="Long title long title long title(560)" />
    </RelativeLayout>
</LinearLayout>

enter image description here

1 Ответ

0 голосов
/ 13 марта 2019

Попробуйте приведенный ниже код, возможно, вам подойдет, где я создал собственный относительный макет

package com.test;

public class MySquareRelativeLayout extends RelativeLayout {

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

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

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

    @TargetApi(VERSION_CODES.LOLLIPOP)
    public MySquareRelativeLayout(Context context, AttributeSet attrs,int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Set a square layout.
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

}

<?xml version="1.0" encoding="utf-8"?>
<com.test.MySquareRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/elementRootView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

  <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_centerInParent="true"/>

  <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_below="@+id/image"
            android:textAlignment="center"
            tools:text="Long title long title long title(560)" />

</com.test.MySquareRelativeLayout>
...