2010-05-06 77 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"/> 
相關問題