如何在RelativeLayout中添加间距

18

我有一个相对布局。

它有两个并排的按钮,且右对齐。

这是我的布局xml文件。 我的问题是最右边的按钮和RelativeLayout右边框之间以及两个按钮之间没有间距。 我该如何添加?我尝试使用android:paddingRight进行调整,但没有效果。

谢谢。

<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="0dp" android:paddingRight="10dp">

    <Button android:id="@+id/1button" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingLeft="10dp" android:paddingRight="10dp"/>

    <Button android:id="@+id/1button" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/1button"
        android:paddingLeft="10dp" android:paddingRight="10dp"/>
4个回答

24

修复ID并尝试使用android:layout_marginRight =“10dip”


8
android:layout_margin="10dp"

或者

android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"

1

你的按钮有重复的ID,请尝试修复并查看是否正常。

除此之外,你的布局看起来很好。然而,如果你解决了ID问题,右侧将会有20个dip的填充(10个来自布局和10个来自按钮)。


0

marginLeft 对我非常有用。我添加了一个空的 TextView 作为间隔,这样下面的所有子元素都可以与上面的按钮对齐。以下是示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_Cancel"
            android:onClick="returnToConnectionList"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"/>
        <TextView
            android:id="@+id/view_Spacer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Label_AddSpacer"
            android:layout_marginLeft="25dp"
            android:layout_toRightOf="@id/btnCancel"
            android:layout_alignParentTop="true"/>

        <Button android:id="@+id/btnSave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_Save"
            android:onClick="saveConnection"
            android:layout_toRightOf="@id/view_Spacer"
            android:layout_alignParentTop="true"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接