How to vertically align text using a button with a highlighted background?

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" /> <!-- pressed -->
    <item android:state_focused="true"
        android:drawable="@drawable/button_pressed" /> <!-- focused -->
    <item android:drawable="@drawable/button" /> <!-- default -->
</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.

+3
2

9 , . Android , , . , 9 , / /. / , , , / , . , / / . , .

, , 9- draw9patch.bat SDK.

+1

, 9 - . , , , .

, , Button , . , , .

+1

Source: https://habr.com/ru/post/1744309/


All Articles