中心文本android按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10978550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:38:46  来源:igfitidea点击:

Center text android button

androidgravity

提问by dieters

My question is very simple. How do I center the text on a button in android? I tried to set padding to 0, gravity to center but the result when i run it still that the text is horizontal centred but not vertical. The text is a bit shifted to the bottom.

我的问题很简单。如何在android中的按钮上居中显示文本?我试图将填充设置为 0,将重力设置为中心,但是当我运行它时,结果仍然是文本水平居中而不是垂直。文本有点移到底部。

<Button
    android:id="@+id/btnSame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="10dip"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:background="@drawable/layout_button_different"
    android:gravity="center_vertical|center_horizontal"
    android:height="30dp"
    android:padding="0dip"
    android:text="@string/equals"
    android:textColor="@drawable/layout_button_different"
    android:textSize="50dp"
    android:width="70dp" />

My be also relevant: In activity I do this:

我也是相关的:在活动中我这样做:

btnEquals.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.up2), null, null);
btnEquals.setPadding(0, 5, 0, 0);
btnEquals.setTextSize(15);

This works but after this I set this:

这有效,但在此之后我设置了这个:

btnEquals.setCompoundDrawables(null, null, null, null);
btnEquals.setPadding(0, 0, 0, 0);

Result is a bad vertical alignment.

结果是错误的垂直对齐。

采纳答案by mah

Your existing layout cannot hope to center the text due to the sizes you have selected. You've set the button height to 30dpand 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.

由于您选择的大小,您现有的布局不能希望将文本居中。您已将按钮高度设置为30dp并且您的 textSize 为50dp,无论出于何种原因,Android 都无法处理并将文本居中。如果您使按钮变大或文本变小,您会看到居中效果。

回答by Cabezas

It's very easy, you can put this:

这很容易,你可以把这个:

android:textAlignment="center"

回答by user2712329

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)

需要考虑三个系统。xml 布局设计器、模拟器和真实手机。如果您指定gravity="center",则按钮中的文本将如下所示。在 xml 布局设计器中,它看起来像最左边的字母在按钮的中心,但其余的字母在右边。(看起来不正确)在模拟器中,文本在中心显示得很好。(正确)在电话中,文字在中心显示得很好。(正确的)

回答by uowaep

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.

您还可以在 setPadding 中使用负数。如果您的字体很大而按钮很小,这可能会有所帮助。如果顶部或底部仍然偏离中心,请尝试将顶部或底部设置为更负的数字。

回答by Shiv Buyya

You can just add this line to your Button in layout xml code:

您可以在布局 xml 代码中将此行添加到您的按钮:

android:gravity="center"

回答by Vipin Sahu

<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

这对我有用