"机器人:layout_margin"或者" android:layout_marginTop"

时间:2017-12-17 22:03:33

标签: android

我正在编写一个屏幕,我希望在两个按钮之间放置一个垂直间隙:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/size40dp"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn_show_cards"
            style="@style/btnStyleAcapulco"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/size50dp"
            android:layout_gravity="center"
            android:text="@string/show_cards" />

        <Button
            android:id="@+id/btn_add_card"
            android:layout_marginTop="@dimen/size40dp"
            style="@style/btnStyleDark_khaki"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/add_card"
            />
    </LinearLayout>

问题是按钮之间没有间隙。但当我更换&#34; android:layout_marginTop&#34;与&#34; android:layout_margin&#34;然后差距是我想要的。根据文字,我读了&#34; layout_marginTop&#34;是正确的选择。知道我做了什么吗?

谢谢,

ZB

4 个答案:

答案 0 :(得分:0)

您对此方案有更多答案。根据您的偏好,您可以使用

1.layout_margin_top =“10dp”来自第二个按钮

第一个按钮

2.layout_margin_bottom =“10dp”

答案 1 :(得分:0)

您的android:layout_margin="10dp"btnStyleAcapulco代码中是否有btnStyleDark_khaki代码?

如果您这样使用,android:layout_marginBottom="40dpandroid:layout_marginTop="40dp将对您的布局无效。

所以你可以检查一下。并删除android:layout_margin="10dp"这样的代码。

第一种方式

1.将android:layout_marginBottom="40dp""添加到按钮btn_show_cards

2.将android:layout_marginTop="40dp"添加到按钮btn_add_card

第二种方式

您可以在两个按钮之间使用Space代码。

<android.support.v4.widget.Space
    android:layout_width="match_parent"
    android:layout_height="40dp"/>

答案 2 :(得分:0)

Second button only declare

android:layout_layout_marginTop=""

答案 3 :(得分:0)

Update your layout like this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match`
    android:layout_marginTop="@dimen/size40dp"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_show_cards"
        style="@style/btnStyleAcapulco"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/size50dp"
        android:layout_gravity="center"
        android:text="@string/show_cards" />

    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="25dp"/>

    <Button
        android:id="@+id/btn_add_card"
        android:layout_marginTop="@dimen/size40dp"
        style="@style/btnStyleDark_khaki"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/add_card" />
</LinearLayout>