Android relative layout problem with gravity
How can i put textview with id="naslov" to the center? I also tried with layout_gravity="center" but that doesn't work either.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/naslov"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="Povzetek"
android:gravity="center"/>
<TextView
android:id="@+id/aha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dip"
android:text="Vseh oddaj:"
android:layout_below="@id/naslov"/>
</RelativeLayout>
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
.
The gravity
works but layout_gravity
doesn't. See my fuller answer for more details.
See also
上一篇: 在一个RelativeLayout中居中放置多个项目,而不将它们放入容器中?
下一篇: Android的重力相对布局问题