Alex Rivera | Logout

How to use the center of a View as a reference point inside a RelativeLayout

Asked 2012-05-25T08:46:59.907
11

I've been using a bit of a hack to stretch a view inside a parent RelativeLayout so that it starts at top-left and ends an bottom-center. While RelativeLayout does let you position something at the center of itself, it doesn't however seem to let you stretch something to that same point.

My solution was simple, position something in that center, and give it 1px dimensions so its the actual center of the parent RelativeLayout. Don't get me wrong, this works and I've had no issues whatsoever with it, but if there's a better practice that this I'd like to know about it.

As for a graphical representation of what I'm talking about, let me add a few images illustrating.

                                                enter image description here

This first image has a text-less TextView positioned at the center of the RelativeLayout and I've given it a width of 0dp. This allows me as you can see in the next image to place anything, relative to that center. My point being that it strikes me as odd that you can't do that without the need of adding the extra view at the center, for I can see properties such as Layout to left of or Layout to right of but no Layout to center of.

                                                 enter image description here

So the scennario may be a bit more in the lines of, "it's not broken yet but I'm afraid it will pop at any minute". Then on the other hand if this is the correct way of doing it and I help someone learning a new thing, that also works for me.

Why do I ask questions that don't get answered!? I'm so setting a bounty here..

The most sensible thing suggested yet (to my judgement) is that I replace m

Edit
Report

1 Answer

1

I think it's better to use LinearLayout. With the attribute 'layout_weight', I think LinearLayout can solve lots of problems. Using LinearLayout also needs another empty view inside the LinearLayout, but I think it is better, I love LinearLayout :) Here is my code

<LinearLayout android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:background="@drawable/your_image">
    <Button android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1.0"/>
    <View android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1.0"
        android:visibility="invisible"/>
</LinearLayout>

At last, sorry for my poor English.

answered 2012-05-27T14:39:25.647

Your Answer