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.
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
.
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 my LinearLayout
with a View
which I would assume takes a little less memory, even if it's marginally less. So I'm thanking @yorkw for it. I'm surprised that nobody's asked this before.
Just to clarify since there seems to be a misunderstanding as to what I'm actually after. I don't want something to take half of the parent width/height. I was asking for a way to use it as a reference point (that's what I said in the title) so I can do stuff like, position an image to the left of the center without adding a linear layout that takes half and the gravity left or whatever.
Update
Since API 14 we have Space
available which is a lightweight View
subclass that may be more suitable for this kind of hack.