Center text android button

荒凉一梦 提交于 2019-12-03 09:36:02

Your existing layout cannot hope to center the text due to the sizes you have selected. You've set the button height to 30dp and your textSize is 50dp, and for whatever reason, Android is unable to deal with that and center the text. If you make your button larger or your text smaller you'll see that the centering works.

It's very easy, you can put this:

android:textAlignment="center"

There are three systems to consider. The xml layout designer, the simulator, and real phone. The text in the button if you specify gravity="center" will look like this. In xml layout designer it will look like the leftmost letter is in the center of the button but the rest of the letters are to the right. (looks incorrect) In simulator, the text shows well in the center. (correct) In phone, the text shows well in the center. (correct)

You can also use negative numbers in setPadding. This may help if your font is large and your button is small. Try also setting the top or bottom to a more negative number if it's still off center.

<Button
                android:id="@+id/AnswerE"
                style="@style/defaultbuttonstyle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:onClick="AnswerCheck"
                android:text=" E "
                android:textStyle="bold" />

This worked for me

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!