java - 无法将两个相对布局中的对象居中

标签 java android xml layout

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

最佳答案

我建议使用 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>

enter image description here

在内部 LinearLayout 中,您可以使用 android:gravity="center" 使其子级垂直和水平居中。或者使用 android:gravity="center_horizo​​ntal" 仅水平居中。

编辑:添加了 ,这在评论中进行了讨论。

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


How can I use android:layout_below="@+id/go_button" in a linear layout?

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

关于java - 无法将两个相对布局中的对象居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18920280/

相关文章:

java - 如何使用 Java 将 JMS 消息排入 Oracle AQ

java - 这是滥用类型转换吗?

android - 在 PackageInstaller 完成( self )更新后启动应用程序

java - Android SQLite 查询从时间字符串列获取不同的年份值

SQL 从 XML 中提取值

xml - cvc-complex-type.2.4.a : Invalid content was found starting with element 'MarkupListURI' . 预期为 '{MarkupDeleteURI}' 之一

java - 从 .class 文件创建实例时出现问题

java - 启动 Java (Eclypse) 时出现奇怪错误

android - 单击 Samsung Galaxy Tab 9.0 版 WebView 中的 SELECT 标记会使应用程序崩溃

java - 沿 TextView 宽度的波纹效果