Gjord83 Gjord83 -4 years ago 168
Android Question

Align TextView to center of RelativeLayout

in my application i have a Relative Layout and inside 3 textview. I would place the 3 textview
, 1 to left, 1 to right and 1 to center.

AS IS

enter image description here

TO BE

enter image description here

THIS MY LAYOUT XML

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#FFFFFF"
>
<TextView

android:id="@+id/girone"
android:layout_width="fill_parent"
android:layout_height="20dip"
style="@style/NomeSquadra"

/>

<TextView

android:layout_marginLeft="2dip"
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="25dip"
android:layout_below="@+id/girone"
style="@style/NomeSquadra"
android:layout_alignParentLeft="true"
android:gravity="left|center_vertical"
/>



<TextView

android:layout_marginLeft="2dip"
android:id="@+id/vs"
android:layout_toRightOf="@+id/home"
android:layout_width="wrap_content"
android:layout_height="25dip"
android:layout_below="@+id/girone"
style="@style/NomeSquadra"
android:gravity="center|center_vertical"
/>



<TextView
android:layout_marginLeft="2dip"
android:id="@+id/away"
android:layout_toRightOf="@+id/vs"
android:layout_width="wrap_content"
android:layout_height="25dip"
android:layout_below="@+id/girone"
style="@style/NomeSquadra"
android:layout_alignParentRight="true"
android:gravity="right|center_vertical"
/>

</RelativeLayout>


@style/NomeSquadra :

<style name="NomeSquadra" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">#C0C0C0</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
<item name="android:textColorHighlight">#C0C0C0</item>
</style>


How place textview with id "vs" to center of the relative layout (center screen)?

thanks

Answer Source

Try this following layout structure with yours...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF" >

    <TextView
        android:id="@+id/girone"
        style="@style/NomeSquadra"
        android:layout_width="fill_parent"
        android:layout_height="20dip" />

    <TextView
        android:id="@+id/home"
        style="@style/NomeSquadra"
        android:layout_width="wrap_content"
        android:layout_height="25dip"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/girone"
        android:layout_marginLeft="2dip"
        android:gravity="left|center_vertical" />

    <TextView
        android:id="@+id/vs"
        style="@style/NomeSquadra"
        android:layout_width="wrap_content"
        android:layout_height="25dip"
        android:layout_below="@+id/girone"
        android:layout_centerHorizontal="true"
        android:gravity="center|center_vertical" />

    <TextView
        android:id="@+id/away"
        style="@style/NomeSquadra"
        android:layout_width="wrap_content"
        android:layout_height="25dip"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/girone"
        android:layout_marginLeft="2dip"
        android:layout_toRightOf="@+id/vs"
        android:gravity="right|center_vertical" />

</RelativeLayout>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download