Button text vertical alignment is off in Android
I'm having trouble with button text alignment in my Android app. I have a button defined in XML like this:
<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
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 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:startC开发者_如何学运维olor="#000"
android:endColor="#919191"/>
</shape>
Try this:
android:includeFontPadding="false"
It helped me with a similar problem.
If you are building your view programmatically, use this:
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
<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.
this worked for me:
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
.
So make sure you do not have it specified or set android:lineSpacingExtra="0dp"
(in case you have to override parent style).
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
精彩评论