2010-05-06 395 views
11

我有一个相对布局。 它有两个并排的按钮,它是右对齐的。如何在RelativeLayout中添加间距

所以这是我的布局xml文件。我的问题是最右边的按钮和RelativeLayout的右边框之间以及2个按钮之间没有间距。我如何添加?我玩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"/> 

回答

21

修复ID和尝试的Android:

1

你有重复按钮的ID,试着修复它,看看它是否看起来不错。

否则,您的布局看起来不错。但是,如果您解决了ID问题,则右侧会有20个dip填充(10个来自布局,10个来自按钮)。

5
android:layout_margin="10dp" 

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

的marginLeft为我工作的伟大layout_marginRight = “10dip”。我添加了一个空的TextView作为一个spacer,所以现在所有的孩子都可以与上面的按钮对齐。这里是一个样本:

<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"/> 
相关问题