Reputation: 707
I have two TextView elements, with changing length. How do I get them to be centered horizontally in an RelativeLayout? The RelativeLayout must wrap_content, as I have views to the left of it.
See my sketch below:
It doesn't have to be a RelativeLayout, as long I can position the two TextViews above each other.
EDIT: Based on the answers I tried this code:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/centerContainer"
android:gravity="center">
<TextView
android:text="Mmmmmmmmmmmm"
android:id="@+id/upperText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="M"
android:id="@+id/lowerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/upperText"/>
</RelativeLayout>
It doesn't work. What am I doing wrong?
Upvotes: 1
Views: 637
Reputation: 1768
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
thats it...
android:layout_centerHorizontal="true" -> if true, center this child horizontally within its parent
Upvotes: 1
Reputation: 11
You must have something like :
<RelativeLayout
some options...
<TextView
some options...
/>
<TextView
some options...
/>
</RelativeLayout>
Add android:gravity = "center"
to your RelativeLayout
options :
<RelativeLayout
some options...
android:gravity = "center"
<TextView
some options...
/>
<TextView
some options...
/>
</RelativeLayout>
This should center every element in your RelativeLayout
.
Upvotes: 0