Java 无法在两个相对布局中居中放置对象

Java 无法在两个相对布局中居中放置对象,java,android,xml,layout,textview,Java,Android,Xml,Layout,Textview,我正在尝试将我的水平布局的UI分成两半,每个都有自己的真实布局,这样我就能够在屏幕的左右两半中居中显示文本和图形。问题是当我想这么做的时候,GO按钮仍然在屏幕中间结束,而不是以屏幕的右边为中心。p> (如果我能找出如何正确居中,我相信我能将同样的技术应用到其他元素上,以达到我想要的效果 资料来源: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.androi

我正在尝试将我的水平布局的UI分成两半,每个都有自己的真实布局,这样我就能够在屏幕的左右两半中居中显示文本和图形。问题是当我想这么做的时候,GO按钮仍然在屏幕中间结束,而不是以屏幕的右边为中心。p> (如果我能找出如何正确居中,我相信我能将同样的技术应用到其他元素上,以达到我想要的效果

资料来源:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="8sp"
    android:layout_marginLeft="8sp"
    android:layout_marginRight="8sp"
    android:layout_marginTop="8sp"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/emblem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="24dp"
            android:gravity="center"
            android:scaleType="fitStart"
            android:src="@drawable/apn_app_logo" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/start2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/go_button"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="@string/start_text2"
            android:textColor="#000000"
            android:textSize="18sp" />

        <Button
            android:id="@+id/go_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/apn_app_go_button" />
    </RelativeLayout>

</RelativeLayout>

编辑:(回应库巴的回答)


我建议使用线性布局,而不是相对布局

     <LinearLayout 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:orientation="horizontal"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:background="#00dd00"
        android:gravity="center"
     >

      <Button 
          android:text="Button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:background="#dd0000"
        android:gravity="center_horizontal"
    >

        <Button 
          android:text="Button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />


    </LinearLayout>

</LinearLayout>

如何在线性布局中使用android:layout_down=“@+id/go_按钮”

不能。这些关系仅在相对布局中起作用,在相对布局中,您可以将视图彼此或父视图相对放置。在线性布局中,项目可以垂直或水平堆叠。

Set
android:gravity=“center”
in

 <Button
            android:id="@+id/go_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/apn_app_go_button" />


可能是它的工作方式。

这不能用于线性布局。这让我可以将go按钮居中…但我似乎无法将其下方的文本居中。如何在线性布局中使用android:layout_down=“@+id/go_按钮”(eclipse说它不能用于线性布局)(我现在只需要将文本居中,因为我可以将go按钮居中)@Wendy我编辑了答案,希望它能解释你所有的问题。
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:background="#dd0000"
    android:gravity="center"
>

    <Button 
      android:text="Button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
    />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text here"    
        />


</LinearLayout>
 <Button
            android:id="@+id/go_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/apn_app_go_button" />