java 按钮文本垂直对齐在 Android 中关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6041514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:05:50  来源:igfitidea点击:

Button text vertical alignment is off in Android

javaandroidbutton

提问by jvergeldedios

I'm having trouble with button text alignment in my Android app. I have a button defined in XML like this:

我在 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"/>

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>

采纳答案by KyleM

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.

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

回答by markussvensson

Try this:

试试这个:

android:includeFontPadding="false"

It helped me with a similar problem.

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

回答by DiscDev

If you are building your view programmatically, use this:

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

button.setIncludeFontPadding(false);

回答by QuarK

That worked for me:

这对我有用:

android:gravity="fill_vertical"

Hope it helps!

希望能帮助到你!

回答by Zayin Krige

this worked for me:

这对我有用:

android:layout_gravity="center_vertical"

回答by Leo Droidcoder

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.
So make sure you do not have it specified or set android:lineSpacingExtra="0dp"(in case you have to override parent style).

我遇到了垂直文本对齐问题。
事实证明,这是因为设置android:lineSpacingExtra.
所以请确保您没有指定或设置它android:lineSpacingExtra="0dp"(以防您必须覆盖父样式)。

回答by Renetik

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.

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

回答by Anup Rojekar

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

~阿努普