中心文字android按钮 [英] Center text android button

查看:65
本文介绍了中心文字android按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单.如何在android中的按钮上居中显示文字?我尝试将padding设置为0,将重力设置为居中,但是运行该文本时,结果仍然是水平居中居中而不是垂直居中.文本移到了底部.

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.

<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.

推荐答案

由于选择的大小,您现有的布局无法希望使文本居中.您已将按钮高度设置为 30dp ,并且textSize为 50dp ,并且由于任何原因,Android无法处理该问题并将文本居中.如果您将按钮变大或文本变小,您将看到居中工作.

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.

这篇关于中心文字android按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆