Android 如何在 RelativeLayout 中添加间距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2784626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add spacing in RelativeLayout
提问by michael
I have a Relative Layout. Which has 2 buttons, side by side and it is right-aligned.
我有一个相对布局。它有 2 个按钮,并排且右对齐。
So this is my layout xml file. My question is there are no spacing between the right-most button and the right border of the RelativeLayout and between the 2 buttons. How can I add that? I play with android:paddingRight, but nothing helps.
所以这是我的布局 xml 文件。我的问题是最右边的按钮和 RelativeLayout 的右边框之间以及 2 个按钮之间没有间距。我该如何添加?我玩 android:paddingRight,但没有任何帮助。
Thank you.
谢谢你。
<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"/>
回答by Alex Volovoy
Fix ids and try android:layout_marginRight="10dip"
修复 ids 并尝试 android:layout_marginRight="10dip"
回答by drawnonward
android:layout_margin="10dp"
or
或者
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
回答by Brandon O'Rourke
You have duplicated ids for the buttons, try fixing that and see if it looks ok.
您有重复的按钮 ID,请尝试修复它并查看它是否正常。
Otherwise, your layout looks good. However, if you fix the ID problem, there will be 20 dip padding on the right (10 from the layout and 10 from the button).
否则,您的布局看起来不错。但是,如果您修复 ID 问题,则右侧将有 20 个浸入填充(10 个来自布局,10 个来自按钮)。
回答by Ramblen Man
the marginLeft worked great for me. I added an empty TextView as a spacer so now all the children below can line up with the buttons above. Here is a sample:
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"/>