I had a simple button configured with a background image defined as
android:background="?attr/button"
where there ?attr/buttonwas a link to a simple 9-patch png. Everything worked fine, the text in the button was correctly aligned.
Then I needed to have a different background for the pressed state of the button. So I changed it to
android:background="@drawable/state_button"
where @drawable/state_buttonis xml with the following states
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" />
<item android:state_focused="true"
android:drawable="@drawable/button_pressed" />
<item android:drawable="@drawable/button" />
</selector>
And after that I can not align the text correctly. If I put it android:gravity="center_vertical", the text will be drawn at about 1/4 of the height of the button at the top.
I double-checked my 9-patch images, everything seems fine with them. And I also tried to have regular png for the background, it also does not change anything.