两行按钮的对齐问题

发布于 2024-11-27 17:16:09 字数 2193 浏览 0 评论 0原文

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

如果我缩短第三个按钮上的文本直到它适合一行,则不会发生这种情况,这告诉我它与换行符有关。将 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>

I'm trying to figure out why a two-line button in my application is being shifted a couple of pixels lower than the other buttons:

An error dialog.  The error message is "Error: No internet connection.  Please check your connection settings and try again."  There are three buttons beneath the error message: Retry, Cancel, and Network Settings.  The Network Setting button is a few pixels lower than the other buttons.

This does not happen if I shorten the text on the third button until it fits on one line, which tells me it has something to do with the line break. Adding android:layout_gravity="top" to the button's layout doesn't seem to help. Any ideas what might be causing this one?

Edit: Here's the layout XML file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一抹苦笑 2024-12-04 17:16:09

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

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

A horizontal LinearLayout aligns the baselines of all its child controls by default. So the first line of text in your multi-line button is vertically aligned with the single line of text in the other buttons.

To disable this behaviour, set android:baselineAligned="false" on the LinearLayout.

断爱 2024-12-04 17:16:09

看过你的布局(在设备上)后,我不确定为什么它会表现出这种奇怪的行为。调试布局时,我喜欢在视图上添加背景颜色,这样您就可以更清楚地看到它们占用的空间。如果我们删除所有填充,我们会发现按钮根本不在同一顶线上。如果我们将 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 您可以让所有按钮位于同一顶部边缘。

Having had a look at your layout (on a device), I am not sure why it exhibits this strange behaviour. When debugging layouts I like to put background colours on Views so you can see more clearly the space they are taking up. If we remove all the padding, we see that the buttons simply don't sit on the same top line. If we apply android:gravity="center_vertical" to the containing LinearLayout we see that the first two buttons are centered but the last one sits snugly with the top edge.

One solution to this is just to rewrite the inner container using 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>

From my testing, by using a RelativeLayout you can get all of the buttons to sit on the same top edge.

遥远的绿洲 2024-12-04 17:16:09

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

I ran your code with the following change and it worked fine:
Change the android:gravity in the horizontal linear layout from "center_horizontal" to "center".

不喜欢何必死缠烂打 2024-12-04 17:16:09

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

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

对我来说,4dp 工作正常,您可以检查是否需要更多或更少。

进行这些更改,您的按钮就会像其他按钮一样正常:-)

I have add the following lines to your third button in XML and make it's height to "fill_parent" :

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

For me, 4dp worked fine you can check if you need more or less.

Make these changes and your button will be fine, like its fellows :-)

倾城月光淡如水﹏ 2024-12-04 17:16:09

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

write android:layout_height="fill_parent" for all three buttons

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文