I have 5 LinearLayouts. For ease of writing and understanding, I'll refer to them as Views A - E.
View A is the parent of Views B, C, D, and E.
I have set the layout_weight to 8.0f for View A and the weights to 1.0f, 3.0f, 1.0f, 3.0f for Views B - E respectively.
I understand that this is for managing the empty space in the parent view, but I was hoping to size my View and subviews to own a percentage of the screen, rather than compete just for the free space.
However, this is all done programmatically, so should I set the height of the LinearLayouts to a coefficient of the getHeight()
method/accessor of it's parent (View A)? If so, then how can I get the height of the parent if the view hasn't yet been added to it's parent, which would set its height with MATCH_PARENT?
Since the getHeight()
method will return 0 for onCreate
, onStart
, and the first onResume
, I had to keep looking for an answer. I found ViewTreeObserver
and OnGlobalLayoutListener
, which will let me know when the Layout
s have been set. However, I would really appreciate having the height before any drawing occurs. Is that not possible?
If you don't need to do this programatically, you can do it in xml. In order to have the child LinearLayouts take up a percentage of the Parent LinearLayout (LinearLayout A) then you need to set the parent's weightSum=(Total layout_weight of child LinearLayouts) and then set the child LinearLayouts width/height property to "0dip" and set their layout_weight to the desired percentage.
Sample code for a vertical orientation would be:
<LinearLayout
android:id="@+id/A"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:weightSum="8.0"
android:orientation="vertical">
<LinearLayout
android:id="@+id/B"
android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"/>
<LinearLayout
android:id="@+id/C"
android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="3.0"
</LinearLayout>