Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序上的按钮间距相等_Java_Android_Xml - Fatal编程技术网

Java 应用程序上的按钮间距相等

Java 应用程序上的按钮间距相等,java,android,xml,Java,Android,Xml,我的应用程序上有2个按钮,当前位置如图所示: 我已更改xml,因此高度属性设置为“0dp”,权重设置为“1”。这为我提供了正确的相等间距,但按钮现在展开以填充此空间,如图所示: 我如何修改我的布局,以便实现所需的布局?(注意:我在paint中创建了此布局) 当前XML布局: Here is my current XML code: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="ht

我的应用程序上有2个按钮,当前位置如图所示:

我已更改xml,因此高度属性设置为“0dp”,权重设置为“1”。这为我提供了正确的相等间距,但按钮现在展开以填充此空间,如图所示:

我如何修改我的布局,以便实现所需的布局?(注意:我在paint中创建了此布局)

当前XML布局:

Here is my current XML code:

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="84dp"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imgLink"
    android:layout_width="78dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:src="@drawable/appointmentmanimenuicon" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:gravity="center"
    android:text="Appointments"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical" >

<Button
    android:id="@+id/btnaddAppoint"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="20dp"
    android:drawableLeft="@drawable/appointmentmenu"
    android:src="@drawable/appointmentmenu"
    android:text="Add Appointment"
    android:layout_weight="1" />

<Button
    android:id="@+id/btnviewAppoint"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="20dp"
    android:drawableLeft="@drawable/appointviewicon"
    android:src="@drawable/appointviewicon"
    android:text="View Appointments"
    android:layout_weight="1" />
</LinearLayout>

</LinearLayout>
以下是我当前的XML代码:

您可以使用layout\u height=“yourvalue”给出布局所需的高度,并使用layout\u margin=“yourvalue”安排布局之间的间距。

一个相当简单的解决方案是使用“虚拟”视图在按钮之间创建所需的间距。然后可以通过调整不同的权重来调整间距。也可以去掉包装这两个按钮的
LinearLayout
——通常不需要以相同的方向嵌套多个LinearLayout

尝试用两个按钮替换
线性布局
,如下所示:

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />

<Button
    android:id="@+id/btnaddAppoint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:drawableLeft="@drawable/appointmentmenu"
    android:src="@drawable/appointmentmenu"
    android:text="Add Appointment" />

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />

<Button
    android:id="@+id/btnviewAppoint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:drawableLeft="@drawable/appointviewicon"
    android:src="@drawable/appointviewicon"
    android:text="View Appointments" />

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />