H
H
Hakito2015-11-28 14:12:31
Android
Hakito, 2015-11-28 14:12:31

Why are view dimensions zero?

I want to make my RelativeLayout, the width of which will always be equal to the height, that is, it will be square. To do this, I inherit from it and override onMeasure

public class SquareRelativeLayout extends RelativeLayout {
    public SquareRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int h = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(h, h);
    }
}

In the markup I write:
<hakito.autosim.views.SquareRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:background="@drawable/btn_back"
    android:clickable="true"
    android:padding="2dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image"
       />
  
</hakito.autosim.views.SquareRelativeLayout>

The Layout turns out to be square, as it should be, but everything that I put inside it is not displayed. It is pressed to the upper left corner, and the dimensions are equal to zero, even if absolute dimensions are specified in the properties.
Please tell me how to fix this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Perelygin, 2015-11-30
@orcDamnar

well in your code i have

<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image"
       />

I absolutely do not see any rules for placement - that's why it will be pressed to the right corner. I also don't see your implementation of onLayout to force it somewhere. In addition, it is very bad to do match_parent for the child and wrap_content for the parent

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question