两行按钮的对齐问题 [英] Alignment issues with two-line button

查看:28
本文介绍了两行按钮的对齐问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚为什么我的应用程序中的两行按钮比其他按钮低几个像素:

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:

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

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?

这是布局 XML 文件:

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>

推荐答案

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

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.

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

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

这篇关于两行按钮的对齐问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆