【问题标题】:Unable To Center Objects Within Two Relative Layouts无法在两个相对布局中居中对象
【发布时间】:2013-09-20 15:17:39
【问题描述】:

我正在尝试将我的水平布局的 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>

【问题讨论】:

    标签: java android xml layout


    【解决方案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" 使其子级垂直和水平居中。或者使用 android:gravity="center_horizontal" 仅以水平方式居中。

    编辑:添加了在 cmets 中讨论过的

    要让 textView 出现在按钮下方,您必须将 LinearLayout 的方向从水平更改为垂直。请参阅the developer site

    内部的 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"?

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

    【讨论】:

    • 太棒了!这使我可以将 go 按钮居中...但我似乎无法将其下方的文本居中。如何在线性布局中使用 android:layout_below="@+id/go_button"? (日食说它不能与线性布局一起使用)
    • (现在我只需要将文本居中,因为我可以将 go 按钮居中)
    • @Wendy 我编辑了答案,希望它能解释你所有的问题。
    【解决方案2】:

    中设置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" />
    

    也许它的工作。

    【讨论】:

    • 这不能用于线性布局
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2012-12-17
    • 1970-01-01
    • 2013-01-29
    相关资源
    最近更新 更多