Skip to content Skip to sidebar Skip to footer

Relativelayout Gravity Not Applied?

I have an activity with the following layout :

Solution 1:

The answer is easy. Gravity works for the content, layout_gravity for the view that uses that. Source basically: https://stackoverflow.com/a/3482757/180538

Try to use LayoutParams with addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

To answer your gravity understanding: You are right but I guess that this information in the documentation is important:

Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.

I can't test your layout at the moment but my guess is that the time where gravity is applied doesn't create the expected result.

Personally I would use gravity only in LinearLayouts and the centerInParent for RelativeLayouts.

Post a Comment for "Relativelayout Gravity Not Applied?"