简体   繁体   English

Android 中的按钮文本垂直对齐已关闭

[英]Button text vertical alignment is off in Android

I'm having trouble with button text alignment in my Android app.我在 Android 应用程序中遇到按钮文本对齐问题。 I have a button defined in XML like this:我有一个用 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"/>

The result is this:结果是这样的:

button.jpg按钮.jpg

I was expecting the text to be centered both vertically and horizontally as it does with all of the other buttons in my application, but for some reason with this one it is offset slightly down.我希望文本能够垂直和水平居中,就像我的应用程序中的所有其他按钮一样,但由于某种原因,它稍微向下偏移。 I can't seem to figure out why its doing this, so any help would be appreciated.我似乎无法弄清楚为什么要这样做,因此将不胜感激。

Edit: Here is the XML for the background of the button:编辑:这是按钮背景的 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>

Try this:尝试这个:

android:includeFontPadding="false"

It helped me with a similar problem.它帮助我解决了类似的问题。

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

button.setIncludeFontPadding(false);

That worked for me:这对我有用:

android:gravity="fill_vertical"

Hope it helps!希望能帮助到你!

Try this: dip will size on different devices much better than sp and declaring no gravity is by default completely centered试试这个: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"/>

Additionally: This exact thing happened to me when the font was too tall for the button try decreasing the size of the font slightly and/or increasing the height of the button.... this solved my issue.另外:当按钮的字体太高时,这正是发生在我身上的事情,尝试稍微减小字体大小和/或增加按钮的高度....这解决了我的问题。

这对我有用:

android:layout_gravity="center_vertical"

S really complete answer is this..真正完整的答案是这个..

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

..in some cases when desired button height is too small compared to font size. ..在某些情况下,与字体大小相比,所需的按钮高度太小。

I had an issue with the vertical text alignment.我在垂直文本对齐方面遇到了问题。
Turned out, it happened because of increased vertical line spacing (at the button's parent style) by setting android:lineSpacingExtra .原来,这是因为通过设置android:lineSpacingExtra增加了垂直行距(在按钮的父样式中)。
So make sure you do not have it specified or set android:lineSpacingExtra="0dp" (in case you have to override parent style).因此,请确保您没有指定或设置android:lineSpacingExtra="0dp" (以防您必须覆盖父样式)。

Im Use this:我用这个:

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

I just add a line break我只是添加一个换行符

use

android:layout_height="wrap_content"

It will definitely fix your issue.它肯定会解决您的问题。

If you find this useful then dont forget to mark it as an answer.如果您觉得这很有用,请不要忘记将其标记为答案。

Best Regards,此致,

~ Anup ~阿努普

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM