如何在LinearLayout中调整两个按钮之间的间距

7
在我的应用程序中,有两个按钮在LinearLayout中垂直排列。我想在两个按钮之间提供一个间隔。请提供解决方案...
我的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:padding="10dp"
  >
<Button
    android:id="@+id/btnAction1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/cool_button"
    android:text = "HiText1"
/>

<Button
    android:id="@+id/btnAction2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/cool_button"
    android:text="HiText2"
    android:layout_below="@id/btnAction1"
/>        

</LinearLayout> 

图片

在此输入图片描述

提前致谢


1
你可以设置 android:layout_marginTop="5dp" 或 android:layout_marginBottom="5dp"。 - KMI
4个回答

17
在第二个按钮的顶部添加一个边距(android:layout_marginTop="50dp"):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:padding="10dp"
 >
<Button
   android:id="@+id/btnAction1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/cool_button"
   android:text = "HiText1"
/>

<Button android:layout_marginTop="50dp"
   android:id="@+id/btnAction2"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/cool_button"
   android:text="HiText2"
   android:layout_below="@id/btnAction1"
/>        

</LinearLayout> 

是的,layout_below 只在 RelativeLayout 中有效,而不是 LinearLayout。 - urveshpatel50
1
实际上,如果您的按钮设置了 background 属性,这是行不通的。我无法让它起作用。 - ACV

6

use android:layout_marginTop="10dp"


3
在第二个按钮上使用android:layout_marginTop="10.0dip"

3
请使用此代码来添加您的第二个按钮。
<Buttonandroid:id="@+id/btnAction2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/cool_button"
android:text="HiText2"
android:layout_marginTop="15dp"
/>

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