简体   繁体   English

Relativelayout中的Textview对齐问题

[英]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? 现在,新文本位于中间,但textView2不是吗? 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. 从您的RelativeLayout删除android:gravity="center_horizontal|top" ,这会弄乱规则。

And to align center all View's use android:layout_centerHorizontal="true" on both View's : 为了使所有View's居中对齐,请在两个View's上使用android:layout_centerHorizontal="true"

<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 试试我的代码为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 由于中央水平布局而无法使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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