Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android relative layout problem with gravity

Tags:

android

layout

 android:layout_centerHorizontal="true"

 android:layout_centerHorizontal="true"

there is also

android:layout_centerInParent="true"

Full list for RelativeLayout attributes is here

Also you can tell your TextView to fill_parent and then set gravity=center on it. So it'll center actual text within textView.


Supplemental Answer

As a more general answer to this question, layout_gravity does not work with the subviews of a RelativeLayout. It is for use with a LinearLayout or FrameLayout. However, the subviews of a RelativeLayout can still use gravity as usual because this is just how a view arranges its own content.

See the comparison in the following image. The green and blue views are TextViews inside of a RelativeLayout.

enter image description here

The gravity works but layout_gravity doesn't. See my fuller answer for more details.

See also

  • Gravity and layout_gravity on Android
  • Positioning views within a RelativeLayout