两行按钮对齐问题两行、按钮、问题

2023-09-05 05:45:41 作者:回眸〝醉倾城

我试图找出为什么在我的应用程序中的两行按钮被移出一对夫妇的像素与其它按钮下:

如果我缩短第三个按钮的文字,直到它在一行,它告诉我它是与换行符适合这不会发生。添加安卓layout_gravity =顶到该按钮的布局似乎并没有帮助。任何想法可能会导致这一个?

编辑:这里的布局XML文件:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:layout_height =WRAP_CONTENT
              机器人:方向=垂直
              机器人:重力=center_horizo​​ntal
              机器人:填充=10dip
              机器人:layout_width =WRAP_CONTENT>

  < TextView的机器人:ID =@ + ID / ERROR_TEXT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginBottom =5dip
            机器人:文本=占位
            机器人:layout_width =WRAP_CONTENT
            机器人:TEXTSIZE =17dip/>

  < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                机器人:layout_height =WRAP_CONTENT
                机器人:方向=横向
                机器人:重力=center_horizo​​ntal
                机器人:填充=10dip
                机器人:layout_width =WRAP_CONTENT>
    <按钮机器人:ID =@ + ID / ok_button
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=@字符串/ OK
            机器人:文字颜色=@彩色/黑白
            机器人:TEXTSTYLE =黑体/>

    <按钮机器人:ID =@ + ID / cancel_button
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginLeft =8DIP
            机器人:文本=@字符串/ cancel_login
            机器人:文字颜色=@彩色/黑白
            机器人:TEXTSTYLE =黑体
            机器人:能见度=水涨船高/>

    <按钮机器人:ID =@ + ID / third_button
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginLeft =8DIP
            机器人:文字颜色=@彩色/黑白
            机器人:TEXTSTYLE =黑体
            机器人:能见度=水涨船高/>
  < / LinearLayout中>
< / LinearLayout中>
 

解决方案 MFC界面按钮控件对齐问题

一个水平的LinearLayout 默认对齐及其所有子控件的基线。因此,在您多行按钮文本的第一行是垂直与文本中的其他按钮的单线对准

要禁用此行为,设置机器人:baselineAligned =假的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:

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>

解决方案

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.