2

У меня возникла потребность в LinearLayout квадратной формы. В интернете нашел такое решение.

public class SquareLinearLayout extends LinearLayout {

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

    public SquareLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int size = width > height ? height : width;
        setMeasuredDimension(size, size);
    }
}

И использование в xml:

<com.dnt.dimantik.gamezfinalv.controllers.SquareLinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="@color/colorAccent">

</com.dnt.dimantik.gamezfinalv.controllers.SquareLinearLayout>

Квадрат получился, проверил это установив цвет фона. Но проблема заключается в том, что элементы, которые я помещаю в мой кастомный SquareLayout не отображаются.

К примеру:

<com.dnt.dimantik.gamezfinalv.controllers.SquareLinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="@color/colorAccent">

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="Hello"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="Hello"/>

</com.dnt.dimantik.gamezfinalv.controllers.SquareLinearLayout>

Ни текст ни кнопка не показываются, один только квадрат. Что делать?

4
  • Он не лишний, он нужны для родительского контейнера, и нет, не совпадает цвет, а если бы даже совпадал, как тогда объяснить что не виден Button
    – Dimantik02
    12 ноя 2018 в 5:21
  • 1
    вы забыли добавить super.onMeasure (widthMeasureSpec, widthMeasureSpec) в класс. 12 ноя 2018 в 7:26
  • Да, все получилось! Спасибо)
    – Dimantik02
    12 ноя 2018 в 7:53
  • Что делать, когда кто то ответил на ваш вопрос
    – pavlofff
    12 ноя 2018 в 8:06

1 ответ 1

2

Вы забыли вызвать super.onMeasure (widthMeasureSpec, widthMeasureSpec).

Приведите код метода к данному виду:

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure (widthMeasureSpec, widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int size = width > height ? height : width;
        setMeasuredDimension(size, size);
    }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.