【问题标题】:How to align buttons?如何对齐按钮?
【发布时间】:2013-11-19 14:11:09
【问题描述】:

如何在 android 中对齐按钮,以便它们在按钮上方、下方和按钮之间具有适当的间距?

当前代码:

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

【问题讨论】:

  • 1.不推荐使用 Fill_parent,请使用 match_parent。 2. 使用 sp 作为边距没有意义。使用 dp 代替。 3. 你想达到什么目的?你想如何对齐这些按钮?如图所示?

标签: android android-layout


【解决方案1】:

更改 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>

【讨论】:

    【解决方案2】:

    使用 RelativeLayout 而不是 LinearLayout。 然后您可以轻松对齐按钮。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 2015-07-02
      • 2017-01-10
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 2020-07-26
      • 2020-10-08
      • 2011-07-12
      相关资源
      最近更新 更多