【问题标题】:Alignment issues with two-line button两行按钮的对齐问题
【发布时间】:2011-10-18 14:08:32
【问题描述】:

我试图弄清楚为什么我的应用程序中的一个两行按钮比其他按钮移动了几个像素:

如果我缩短第三个按钮上的文本直到它适合一行,这不会发生这种情况,这告诉我它与换行符有关。将android:layout_gravity="top" 添加到按钮的布局似乎没有帮助。有什么想法可能导致这个问题吗?

编辑:这是布局 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:gravity="center_horizontal"
              android:padding="10dip"
              android:layout_width="wrap_content">

  <TextView android:id="@+id/error_text"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            android:text="Place holder"
            android:layout_width="wrap_content"
            android:textSize="17dip"/>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_horizontal"
                android:padding="10dip"
                android:layout_width="wrap_content">
    <Button android:id="@+id/ok_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ok"
            android:textColor="@color/black"
            android:textStyle="bold"/>

    <Button android:id="@+id/cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dip"
            android:text="@string/cancel_login"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:visibility="gone"/>

    <Button android:id="@+id/third_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dip"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:visibility="gone"/>
  </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 你能发布布局 XML 文件吗?

标签: android android-layout android-button


【解决方案1】:

查看了您的布局(在设备上),我不确定它为什么会表现出这种奇怪的行为。在调试布局时,我喜欢将背景颜色放在视图上,以便您可以更清楚地看到它们占用的空间。如果我们删除所有填充,我们会看到按钮根本不在同一条顶线上。如果我们将android:gravity="center_vertical" 应用于包含LinearLayout,我们会看到前两个按钮居中,但最后一个按钮紧贴顶部边缘。

对此的一种解决方案是使用RelativeLayout 重写内部容器:

<RelativeLayout
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_width="wrap_content">

<Button android:id="@+id/ok_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ok"
        android:textColor="@color/black"
        android:textStyle="bold"/>

<Button android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dip"
        android:layout_toRightOf="@id/ok_button"
        android:text="@string/cancel_login"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:visibility="gone"/>

<Button android:id="@+id/third_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dip"
        android:layout_toRightOf="@id/cancel_button"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:visibility="gone"/>
</RelativeLayout>

根据我的测试,通过使用RelativeLayout,您可以让所有按钮都位于同一顶部边缘。

【讨论】:

    【解决方案2】:

    我通过以下更改运行了您的代码,它运行良好: 将水平线性布局中的 android:gravity 从“center_horizo​​ntal”更改为“center”。

    【讨论】:

      【解决方案3】:

      我已将以下几行添加到 XML 中的第三个按钮,并将其高度设为“fill_parent”:

      android:paddingTop="4dp"
      android:layout_height="fill_parent"
      

      对我来说,4dp 效果很好,您可以检查是否需要更多或更少。

      进行这些更改,您的按钮就可以了,就像它的伙伴一样 :-)

      【讨论】:

        【解决方案4】:

        默认情况下,水平的LinearLayout 对齐其所有子控件的基线。因此,多行按钮中的第一行文本与其他按钮中的单行文本垂直对齐。

        要禁用此行为,请在 LinearLayout 上设置 android:baselineAligned="false"

        【讨论】:

        • 正中靶心! @Amanda S,你应该接受这个作为正确答案。
        • 谢谢,这个答案为我节省了很多时间!
        • 谢谢,没听说过这个问题。
        【解决方案5】:

        为所有三个按钮编写 android:layout_height="fill_parent"

        【讨论】:

          猜你喜欢
          • 2017-07-24
          • 2017-05-07
          • 1970-01-01
          • 2018-04-17
          • 2012-02-01
          • 2013-02-26
          • 2017-01-06
          相关资源
          最近更新 更多