【问题标题】:Button text vertical alignment is off in Android按钮文本垂直对齐在 Android 中已关闭
【发布时间】:2011-05-18 07:56:04
【问题描述】:

我的 Android 应用中的按钮文本对齐有问题。我在 XML 中定义了一个按钮,如下所示:

<Button
        android:id="@+id/reportanissuebutton"
        android:layout_width="272sp"
        android:layout_height="32sp"
        android:text="@string/button_report_issue"
        android:textColor="#fff"
        android:textSize="18sp"
        android:gravity="center_vertical|center_horizontal"
        android:background="@xml/report_issue_button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40sp"/>

结果是这样的:

button.jpg

我希望文本像我的应用程序中的所有其他按钮一样垂直和水平居中,但由于某种原因,它略微向下偏移。我似乎无法弄清楚它为什么这样做,所以任何帮助将不胜感激。

编辑: 这是按钮背景的 XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="6dp" />
    <stroke android:color="#c2c2c2" android:width="2dp" />
    <gradient 
        android:angle="90"
        android:startColor="#000"
        android:endColor="#919191"/>
</shape>

【问题讨论】:

  • 文字已经居中了……你还想改什么?
  • 它没有垂直居中,顶部空间比底部空间大。
  • 您是否尝试过完全删除重力标签或仅使用垂直标签?
  • 我猜你可以尝试设置填充
  • @Rosalie:我设置了android:paddingBottom="3sp",这似乎使它排列正确。不过,我仍然很想知道为什么这是整个应用程序中唯一无法自动对齐文本的按钮。

标签: java android button


【解决方案1】:

试试这个:

android:includeFontPadding="false"

它帮助我解决了类似的问题。

【讨论】:

  • 这以及将 paddingTop 和 paddingBottom 设置为 0dp 都有帮助。
  • 终于有聪明人说出了正确的答案,而不是每个安卓开发者都知道的事情!
【解决方案2】:

如果您以编程方式构建视图,请使用:

button.setIncludeFontPadding(false);

【讨论】:

    【解决方案3】:

    这对我有用:

    android:gravity="fill_vertical"
    

    希望对你有帮助!

    【讨论】:

    • 工作就像一个魅力
    【解决方案4】:

    试试这个:dip 在不同设备上的大小会比 sp 好得多,并且默认情况下声明没有重力是完全居中的

    <Button
        android:id="@+id/reportanissuebutton"
        android:layout_width="272dip"
        android:layout_height="32dip"
        android:text="@string/button_report_issue"
        android:textColor="#fff"
        android:textSize="18dip"
        android:background="@xml/report_issue_button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40sp"/>
    

    另外:当字体对于按钮来说太高时,这件事发生在我身上,尝试稍微减小字体大小和/或增加按钮的高度......这解决了我的问题。

    【讨论】:

      【解决方案5】:

      这对我有用:

      android:layout_gravity="center_vertical"
      

      【讨论】:

        【解决方案6】:

        真正完整的答案是这个..

           <item name="android:includeFontPadding">false</item>
           <item name="android:paddingTop">0dp</item>
           <item name="android:paddingBottom">0dp</item>
        

        ..在某些情况下,所需的按钮高度与字体大小相比太小。

        【讨论】:

          【解决方案7】:

          我遇到了垂直文本对齐问题。
          原来,这是因为通过设置android:lineSpacingExtra 增加了垂直行距(在按钮的父样式)。
          因此,请确保您没有指定或设置android:lineSpacingExtra="0dp"(以防您必须覆盖父样式)。

          【讨论】:

            【解决方案8】:

            我用这个:

                <com.google.android.material.button.MaterialButton
                android:minWidth="1dp"
                android:gravity="center_horizontal"
                android:text="O\nP\nC\nO\nN\nE\nS"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            

            我只是添加一个换行符

            【讨论】:

              【解决方案9】:

              使用

              android:layout_height="wrap_content"
              

              它肯定会解决你的问题。

              如果您觉得这很有用,请不要忘记将其标记为答案。

              最好的问候,

              ~阿努普

              【讨论】:

              • 不,这就是现在的样子:link 我还将在原始问题中添加用于按钮背景的 XML。
              • 不确定我是否理解您的问题,我在上面的评论中发布的图片显示了将android:layout_height="32sp" 替换为android:layout_height="wrap_content" 的结果
              猜你喜欢
              • 2013-03-07
              • 1970-01-01
              • 2012-12-03
              • 2011-06-13
              • 2017-06-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-07-02
              相关资源
              最近更新 更多