0

I have done some experiments with Relative Layout gravity in Android Studio. I am very perplexed with results gotten. Let's look at my xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top" >
    <Button
        android:text="Apple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="20dip" />
</RelativeLayout>

When I set android:gravity to left, right, or bottom I get the proper gravity effects. The view is moved to left, right or bottom correspondingly. However if I set android:gravity to top I get no any visible effect.

There are several other oddities for me. Value left, right, end moves the view but value start doesn't. Why? IMHO if we use LTR the "start" has to be equal to the left. I know that value start|top is the default value for gravity but it doesn't help me to understand the problem. Please help me to clarify the situation. Thank you.

vollitwr
  • 429
  • 2
  • 8
  • I recommend that you focus on `ConstraintLayout`. I am not aware of anything that `RelativeLayout` offered that `ConstraintLayout` fails to offer. `ConstraintLayout` is where Google is investing in ongoing improvements and bug fixes. And `ConstraintLayout` comes from a library, so your results can be more consistent across Android OS versions. – CommonsWare Oct 21 '19 at 11:05
  • With regards to your specific problem, it is possible that you are the first person to ever try using `android:gravity` with `RelativeLayout`. `android:gravity` was added for other types of containers (`FrameLayout`, `LinearLayout`, etc.). You do not need it with `RelativeLayout`, as with `RelativeLayout` you use more explicit rules to specify the sizing and positioning of its children. I am not certain that the rules for `android:gravity` on `RelativeLayout` are well-defined. – CommonsWare Oct 21 '19 at 11:07

0 Answers0