unexpectedUser
unexpectedUser

Reputation: 63

Missing Views inside a Custom RelativeLayout

I got a problem with an modified android layout. After I extends the normal relative layout so that it will be automatically square sized, all embedded other views will disappear.

      <namespace.SquareRelativeLayout
            android:layout_weight="1"
            android:id="@+id/firstBtn"
            android:background="#ff0000"
            android:clickable="true" 
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/firstBtnImage"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/firstBtnDrawable" />
            <TextView
                android:id="@+id/firstBtnLabel"
                android:layout_alignParentBottom = "true"
                android:layout_centerHorizontal="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="10dp"
                android:textColor="#ffffff"
                android:text="@string/firstBtnlabel"
                android:textStyle="bold" />
        </namespace.SquareRelativeLayout>

But Text view and Image view were not shown, even they were anywhere at the screen.

I am be thankful for every hind.

Edit:

   namespace namespace
{
    class SquareRelativeLayout : RelativeLayout
    {
        public SquareRelativeLayout (Context context, IAttributeSet attrs, int defStyle) : base (context, attrs, defStyle)
        {
        }

        public SquareRelativeLayout (Context context, IAttributeSet attrs) : base (context, attrs)
        {
        }

        public SquareRelativeLayout (Context context) : base (context)
        {

        }

        protected override void OnMeasure (int widthMeasureSpec, int heightMeasureSpec)
        {

            //Get canvas width
            int w = MeasureSpec.GetSize (widthMeasureSpec);
            base.SetMeasuredDimension (w, w);
        }
    }
}

Upvotes: 1

Views: 604

Answers (1)

unexpectedUser
unexpectedUser

Reputation: 63

I solved it, after reading a hundred or more site I got a better understanding of android views and the layoutInflater.

But the solution is easy just replacing:

    int w = MeasureSpec.GetSize (widthMeasureSpec);
    base.SetMeasuredDimension (w, w);

with

base.OnMeasure (widthMeasureSpec, widthMeasureSpec);

This keeps the view group of parent and make it possible to add childs.

Upvotes: 2

Related Questions