简体   繁体   English

如何对齐按钮?

[英]How to align buttons?

How can buttons be aligned in android so that they have proper spacing above, below, and between buttons? 如何在Android中对齐按钮,使其在按钮的上方,下方和之间有适当的间距?

Current code: 当前代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<include 
     android:id="@+id/ActionBackupBar"
     layout="@layout/actionbar_layout" 
    />

  <Button
      android:id="@+id/btnSelectLocation"
      style="@style/ButtonText"
      android:layout_width="200dp"
      android:layout_height="wrap_content"
      android:layout_marginLeft="5sp"
      android:layout_marginRight="2sp"
      android:layout_marginTop="0sp"
      android:text="@string/activitySelectMyLocationButton" />

<Button
    android:id="@+id/btnCurrentLocation"
    style="@style/ButtonText"
    android:text="@string/activitySelectSearchLocationButton"
    android:layout_marginBottom="5sp"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="2sp"
    />

在此处输入图片说明

Change for RelativeLayout and after try this: 更改为RelativeLayout,然后尝试以下操作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button
    android:id="@+id/btnSelectLocation"
    style="@style/ButtonText"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:text="text1" />
<Button
    android:id="@+id/btnCurrentLocation"
    style="@style/ButtonText"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/btnSelectLocation"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:text="test2" />
</RelativeLayout>

Use RelativeLayout instead of LinearLayout. 使用RelativeLayout而不是LinearLayout。 Then you can easily align the buttons. 然后,您可以轻松对齐按钮。

Hope this can help you. 希望这可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM