layout_marginTop和layout_marginBottom不适用于嵌套的RelativeLayout

时间:2017-02-21 22:03:24

标签: android

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

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按钮的layout_marginTop。但是将layout_marginBottom添加到email_sign_in_button按钮也不会创建边距。为什么嵌套的RelativeLayout不显示边距?enter image description here

1 个答案:

答案 0 :(得分:1)

发生了什么

父级是RelativeLayout(给定layout_ *参数)。

登录按钮位于顶部。 正下方(layout_below)使用Facebook登录按钮。这两个按钮构成了整个边界框。

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

RelativeLayout不是LinearLayout,这可能是你应该使用的。

如何处理

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

您可以稍后担心优化。

注释

  • 当然添加缺少的android:layout_heightandroid:layout_width属性。
  • 使用android:layout_weight 始终时,将计算尺寸指定为0dp
  • android:layout_gravity视图放在父级中。
  • android:gravity视图的孩子进行了定位。