首页 > 解决方案 > 在父级中居中嵌套相对布局

问题描述

我必须为空屏幕制作(某种)页脚。我的父布局是RelativeLayout1. 在这个布局里面我有ToolBar,下面是另一个RelativeLayout2.

这里面RelativeLayout2是 Button 和另一个RelativeLayout3. 我是这样做的,因为我想将RelativeLayout3内容居中RelativeLayout2并且Button应该始终对齐父布局的底部(RelativeLayout2

我之前得到了类似问题的答案,但它并不总是有效。我现在的问题是RelativeLayout3垂直偏离中心(它更接近底部)。

它应该是什么样子

它应该是什么样子

它实际上看起来如何

它实际上看起来如何

如何解决这个问题?

完整布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/colorBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/toolbar_round"
        android:orientation="horizontal">

        <include
            android:id="@+id/toolBarContent"
            layout="@layout/order_toolbar_layout" />

    </android.support.v7.widget.Toolbar>

    <RelativeLayout
        android:id="@+id/order_complete_footer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolBar"
        android:gravity="center"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/order_complete_image_text_layout"
            android:layout_centerInParent="true"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/order_complete_image"
                android:layout_width="175dp"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:src="@drawable/footer_image" />

            <TextView
                android:id="@+id/order_complete_text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/order_complete_image"
                android:layout_marginEnd="50dp"
                android:layout_marginStart="50dp"
                android:layout_marginTop="-12dp"
                android:gravity="center_horizontal"
                android:text="@string/order_complete_screen_text1"
                android:textColor="@color/colorItemMinor"
                android:textStyle="bold"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/order_complete_text2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/order_complete_text1"
                android:layout_marginEnd="50dp"
                android:layout_marginStart="50dp"
                android:gravity="center_horizontal"
                android:text="@string/order_complete_screen_text2"
                android:textColor="@color/colorItemMinor"
                android:textSize="12sp" />
        </RelativeLayout>

        <Button
            android:id="@+id/order_complete_continue_button"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="@dimen/address_creation_button_padding_TOP_BOTTOM"
            android:layout_marginEnd="@dimen/address_creation_button_padding_START_END"
            android:layout_marginStart="@dimen/address_creation_button_padding_START_END"
            android:layout_marginTop="@dimen/address_creation_button_padding_TOP_BOTTOM"
            android:background="@drawable/login_button_background_void"
            android:text="@string/order_complete_screen_button_label"
            android:textAppearance="@style/VoidLoginButtonTextAppearance" />

    </RelativeLayout>

</RelativeLayout>

标签: androidandroid-layoutkotlin

解决方案


如果我理解正确,您应该将以下内容添加到您的order_complete_image_text_layout.

android:layout_above="@id/order_complete_continue_button" 

@JanStoltman


推荐阅读