0

我试图将我的水平布局的 UI 分成两半,每一半都有自己的真实布局,这样我就可以在屏幕的左右两半内居中放置文本和图形。问题是当我尝试这样做时 - 开始按钮仍然在屏幕中间而不是在屏幕右侧居中。

(如果我能弄清楚如何正确居中,我相信我将能够将相同的技术应用于其他元素以达到我想要的结果。

资源:

<?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>

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

<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/background"
        android:gravity="center"
        android:orientation="horizontal" >

        <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_marginTop="24dp"
            android:gravity="center"
            android:scaleType="fitStart"
            android:src="@drawable/apn_app_logo" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/background"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/go_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/apn_app_go_button"
            android:gravity="center_horizontal" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/start_text2"
            android:textColor="#000000"
            android:textSize="18sp" />
    </LinearLayout>

</LinearLayout>
4

2 回答 2

1

我建议使用 LinearLayout 而不是相对布局。

     <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>

在此处输入图像描述

在内部 LinearLayout 中,您可以使用android:gravity="center"它的子元素垂直和水平居中。或者使用 useandroid:gravity="center_horizontal"仅以水平方式居中。

编辑:添加了在评论中讨论的

要让 textView 出现在按钮下方,您必须将 LinearLayout 的方向从水平更改为垂直。请查看开发者网站

因此,内部的 LinearLayout 看起来像这样。

<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>


如何在线性布局中使用 android:layout_below="@+id/go_button"?

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

于 2013-09-20T15:26:28.040 回答
0

设置android:gravity="center"

 <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" />

也许它的工作。

于 2013-09-20T15:27:21.717 回答