Question

I have some code that should center a text in button, but the text is not aligned exactly in the middle. It is located a little at the bottom. How I could fix this?

</style>
    <style name="ButtonText">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">34dp</item>
    <item name="android:layout_weight">0.5</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:padding">1dp</item>
    <item name="android:textSize">26sp</item>
    <item name="android:textStyle">bold</item>
</style>

        <Button
        android:id="@+id/buttonStop"
        android:background="@drawable/greenbutton"
        style="@style/ButtonText"
        android:text="@string/ButtonStop" />

Screenshot

Was it helpful?

Solution

This is normal. Text generally has top/bottom padding that isn't even. It's used for ascender/descender area.

If you need pixel-perfect centering, you can try telling it not to include the font padding with:

android:includeFontPadding="false"

OTHER TIPS

I guess must be contradict in your style. Change your padding and layout_height attributes to following.

<item name="android:layout_height">wrap_content</item>
<item name="android:paddingTop">4dp</item>
<item name="android:paddingBottom">4dp</item>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top