【问题标题】:How can I add spacing in RelativeLayout如何在 RelativeLayout 中添加间距
【发布时间】:2010-05-06 21:24:54
【问题描述】:

我有一个相对布局。 它有 2 个按钮,并排,它是右对齐的。

这是我的布局 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"/>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    修复 id 并尝试 android:layout_marginRight="10dip"

    【讨论】:

      【解决方案2】:
      android:layout_margin="10dp"
      

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

      【讨论】:

        【解决方案3】:

        您有重复的按钮 ID,尝试修复它,看看它是否正常。

        否则,您的布局看起来不错。但是,如果您修复 ID 问题,右侧将有 20 个 dip padding(10 个来自布局,10 个来自按钮)。

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 2018-02-03
            • 1970-01-01
            • 1970-01-01
            • 2013-09-15
            • 2011-09-07
            • 2015-09-27
            • 1970-01-01
            • 2020-04-08
            • 1970-01-01
            相关资源
            最近更新 更多