Android layout_marginTop和layout_marginBottom不适用于嵌套的RelativeLayout

Android layout_marginTop和layout_marginBottom不适用于嵌套的RelativeLayout,android,Android,我有一个嵌套的相对论。我想添加上页边距和下页边距。我指定为属性: android:layout_marginTop="28dp" android:layout_marginBottom="28dp" 以下是更多的上下文: <Button android:id="@+id/email_sign_in_button" style="?android:textAppearanceSmall" android:layout_width="match_parent"

我有一个嵌套的相对论。我想添加上页边距和下页边距。我指定为属性:

android:layout_marginTop="28dp"
android:layout_marginBottom="28dp"
以下是更多的上下文:

<Button
    android:id="@+id/email_sign_in_button"
    style="?android:textAppearanceSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/inputpassword"
    android:layout_marginTop="16dp"
    android:text="@string/action_sign_in"
    android:background="#fff"
    android:textSize="12sp"
    android:minHeight="36dp"/>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="28dp"
    android:layout_marginBottom="28dp"
    android:layout_centerVertical="true"
    android:layout_below="@+id/email_sign_in_button"
>

<TextView
    android:id="@+id/tvText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:text="OR"
    android:textColor="#000"/>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@id/tvText"
    android:background="@color/material_blue_grey_800"
/>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@id/tvText"
    android:background="@color/material_blue_grey_800"
/>

</RelativeLayout>

<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="28dp"
    android:minHeight="119dp" android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_below="@+id/email_sign_in_button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

但没有增加保证金。当我删除这两个属性时,facebook按钮的布局将显示。但是,在“电子邮件”“登录”“按钮”中添加“版面”“边距”按钮也不会创建边距。为什么嵌套的RelativeLayout不显示边距?

发生了什么 父对象是一个
相对对象
(给定布局参数)

登录按钮位于顶部。它的正下方(下面的布局)是使用Facebook登录按钮。这两个按钮构成了整个边界框

在此边界框中是另一个垂直居中的
RelativeLayout
,因此其边距将被忽略或产生不需要的结果。此
RelativeLayout
与任何按钮之间没有关系

RelativeLayout
不是您应该使用的
线性布局

怎么办

您可以在以后担心优化问题

笔记
  • 当然,添加缺少的
    android:layout\u height
    android:layout\u width
    属性
  • 使用
    android:layou weight
    时,始终将计算的尺寸指定为
    0dp
  • android:layout\u gravity
    将此视图定位在其父视图中
  • android:gravity
    定位此视图的子视图
<LinearLayout android:orientation="vertical">
    <Button/>
    <LinearLayout android:gravity="center_vertical"> <!-- Horizontal by default. -->
        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1"/>
        <TextView
            android:paddingLeft="10dp"
            android:paddingRight="10dp"/>
        <!-- DON'T use horizontal *margins* in horizontal LinearLayout! -->
        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1"/>
    </LinearLayout>
    <LoginButton/>
</LinearLayout>