简体   繁体   中英

Textview alignment issue in Relativelayout

When I have this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center_horizontal|top"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="nl.dylaan.deroosterapp.MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/app_version"
        android:textColor="@android:color/white"
        android:textSize="17sp" />



</RelativeLayout>

The text is in the center:

在中心

But when I add a new text to it:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center_horizontal|top"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="nl.dylaan.deroosterapp.MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/app_version"
        android:textColor="@android:color/white"
        android:textSize="17sp" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="33dp"
        android:text="@string/app_name_cap"
        android:textColor="@android:color/white"
        android:textSize="35sp" />
</RelativeLayout>

The new text is now in the center but the textView2 is not? I need to have it both in center: 在此处输入图片说明

Does anyone knows why this is not working?

Remove android:gravity="center_horizontal|top" from your RelativeLayout , it's messing up with the rules.

And to align center all View's use android:layout_centerHorizontal="true" on both View's :

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/app_version"
    android:textColor="@android:color/white"
    android:textSize="17sp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="33dp"
    android:text="@string/app_name_cap"
    android:layout_centerHorizontal="true"
    android:textColor="@android:color/white"
    android:textSize="35sp" />

Try my code for textview2

   <TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:gravity="center"
    android:text="@string/app_version"
    android:textColor="@android:color/white"
    android:textSize="17sp" />

It doesn't work because of the center horizontal layout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM