In case I have 2 views and I want one to take the necessary space and the other to take the remaining space, how should I go about it?
Let's suppose I want to lay the views vertically. The view below should take the necessary space (wrap_content) and the view above should take the remaining space of the container layout.
I've used these 2 solutions (simplified code):
1) LinearLayout
with weight
<LinearLayout ...>
<View height="0dp" weight="1" .../>
<View height="wrap_content" .../>
</LinearLayout>
2) RelativeLayout
with aligning
<RelativeLayout ...>
<View height="wrap_content" id="@+id/big" alignParentTop .../>
<View height="wrap_content" below="@+id/big" alignParentBottom .../>
</RelativeLayout>
TheLinearLayout
approach always work, and the RelativeLayout
usually works as expected but it is obviously ambiguous, since nothing says that the @+id/big
view should be bigger than the one below.
I think the first approach is better, since is not ambiguous. However, I have seen many examples and answers with the second approach.
What solution do you use for these cases. Is there a best practice?
Thanks!
EDIT
Taking Touf's answer, now I would do this (note the match_parent
):
<RelativeLayout ...>
<View height="match_parent" id="@+id/big" alignParentTop .../>
<View height="wrap_content" below="@+id/big" alignParentBottom .../>
</RelativeLayout>