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>
4

5 回答 5

89

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

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

于 2011-11-28T00:21:28.927 回答
4

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

对此的一种解决方案是使用 a 重写内部容器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>

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

于 2011-08-02T21:08:33.847 回答
2

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

于 2011-08-02T21:27:56.573 回答
2

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

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

对我来说,4dp 工作得很好,你可以检查你是否需要更多或更少。

进行这些更改,您的按钮会很好,就像它的伙伴一样:-)

于 2011-08-03T04:53:46.227 回答
0

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

于 2012-05-19T11:27:26.033 回答