2017-07-18 65 views
-1

我是在android上编程的新手,所以我很难理解相对布局是如何工作的。我已经得到了大部分,但android:layout_centerVertical =“true”只是当我尝试在我的代码中使用它没有工作。我应该按照从上到下的顺序排列3个textviews,所以它会显示“祝你生日快乐”,但我不能让“生日”文本视图居中。当我使用android:layout_centerInParent =“true”时,它的中心位于中间,我无法向左或向右移动它。有人能看我的代码并告诉我我做错了什么吗?谢谢。需要android的帮助:layout_centerVertical =“true”

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:textAppearance="?android:textAppearanceLarge" 
    android:text="Happy" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:layout_alignParentLeft="true" 
    android:textAppearance="?android:textAppearanceLarge" 
    android:text="Birthday" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentBottom="true" 
    android:textAppearance="?android:textAppearanceLarge" 
    android:text="To You" /> 
+1

当您使用相对布局时,您可以为每个视图提供id,然后针对此视图安排其他视图。使用“android:layout_below”作为'生日'和'给你' –

+0

你可以添加一张图片来描述你想要的布局吗? –

回答

0

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:gravity="center" // settext gravity as per your requirement 
     android:text="Happy" 
     android:textAppearance="?android:textAppearanceLarge" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerInParent="true" 
     android:gravity="center"// settext gravity as per your requirement 
     android:text="Birthday" 
     android:textAppearance="?android:textAppearanceLarge" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:gravity="center" // settext gravity as per your requirement 
     android:text="To You" 
     android:textAppearance="?android:textAppearanceLarge" /> 

</RelativeLayout> 
+0

谢谢!我欣赏的帮助 – Sara

+0

@Sara最欢迎的,请接受我答,因此它可以帮助其他也 –

0

您可以android:gravity="center"

将尝试在其容器在垂直 和水平轴中心的对象,而不是改变它的大小。

android:layout_width="match_parent" 
android:gravity="left|center" // Text will Center & left align 

FYI

您正在使用RelativeLayout。为了您的的TextView垂直问题,您应该使用android:layout_below

将此视图的顶部边缘放置在给定锚点视图ID下方。 容纳此视图的顶部边距和锚点视图的底部边距。

0

使用layout_centerVertical="true"在垂直方向

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Happy" 
     android:textAppearance="?android:textAppearanceLarge" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:text="Birthday" 
     android:textAppearance="?android:textAppearanceLarge" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:text="To You" 
     android:textAppearance="?android:textAppearanceLarge" /> 
</RelativeLayout> 
+0

我试着用你提供的代码, Sara

0

到中心视图只需使用android:gravity="center"父布局

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:textAppearance="?android:textAppearanceLarge" 
     android:text="Happy" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_alignParentLeft="true" 
     android:textAppearance="?android:textAppearanceLarge" 
     android:text="Birthday" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentBottom="true" 
     android:textAppearance="?android:textAppearanceLarge" 
     android:text="To You" /> 

</RelativeLayout> 
0

你的文件现在产生正确的是:

Before

你想

而且我认为是这样的:

enter image description here

所以只需添加在你的根布局以下行,这个水平对齐的所有儿童的意见在其中心

android:gravity =“center_horizo​​ntal”

+0

感谢您的帮助!我终于搞定了 – Sara

+0

没问题......! –